Battleship

cout << "Hello, and this is sppsly. It will be my first post please be gentle.";

Now, down to buisness, I've been trying to make a battleship game and it just isn't working out. I've tried everything that I know! Any and all help would be appreciated on the topic.

Thank you,
SppSly
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//
//  main.cpp
//  battleship
//
//  Created by Home on 3/30/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include <iostream>
#include <fstream>
#include <exception>
#include <ctype.h> 


void printinfo();
void printfield(char array[6][6]); 
void printlane(int i, char array[6][6]); 
bool check(int i, int j, char map[6][6]); 
int charcovert(char c); 

int main(){
    char c; 
    int row, collumn = 7, ship = 0, quess = 0; 
    bool sunk = false; 
    std::ifstream ship_location;
    ship_location.exceptions(std::ifstream::failbit | std::ifstream::badbit);
    // [row] [collumn] 
    char field[6][6], player[6][6] = {}; 
    //open file
    try{
        ship_location.open("/Users/home/Desktop/battleship/battleship/ship.txt");
    }
    //catch error
    catch (std::ofstream::failure &e){
        std::cout << "An error has occured";
        return 1; 
    }
    //upload array
        for(int j = 0; j < 6; j++){
    for(int i = 0; i < 6; i++){
        ship_location >> field[j][i]; 
    }
}
    ship_location.close(); 
    printinfo();
    //player guessing, all user input in the while loop    
    while(!sunk){
        printfield(player); 
        
        std::cout << "enter in your location!(q to exit)";
        std::cin >> c >> row;
        c = tolower(c); 
        if(c == 'q') sunk = true; 
        collumn = charcovert(c); 
        ++quess; 
        if(check(row, collumn, field)){
            player[--row][--collumn] = 'H';
            ship++; 
        }
        else {
            player[--row][--collumn] = 'M'; 
        }
        if(ship == 3) sunk = true; 
    }
    printfield(player); 
    std::cout << "YOU HAVE WON!!!! it took you " << quess << " tries";
    return 0;
}

int charcovert(char c){ 
    if(c == 'a') return 1;
    if(c == 'b') return 2;
    if(c == 'c') return 3; 
    if(c == 'd') return 4;
    if(c == 'e') return 5;
    if(c == 'f') return 6;
    if(c == 'q') return 0; 
    else return 7; 
}

bool check(int i, int j, char field[6][6]){
    if(field[i][j] == 'S') return true;
    else return false; 
}

void printfield(char array[6][6]){
    std::cout << "\n  A B C D E F\n";
    for(int i = 0; i < 6; i++){
        std::cout << i + 1 << " "; printlane(i, array); 
        std::cout << '\n'; 
    }
}

void printlane(int i, char array[6][6]){
    for(int j = 0; j < 6; j++){
        std::cout << array[i][j] << " ";
    }
}

void printinfo(){
    std::cout << "********************";
    std::cout << "\n*                  *";
    std::cout << "\n*     BATTLESUB!   *";
    std::cout << "\n*     by me!       *"; 
    std::cout << "\n*                  *";
    std::cout << "\n********************"; 
}
i did this a while ago. its a simple consul 1 player battle ship game. there is no ai in this one although i have one with that if you want like to see that code.
It would be awesome to see it. Thank you so much for helping with this!
sorry it will take several hours to post my AI code. defragmenting my windows partition(46%fragmented somehow) and the code is on my mac partition.
Don't sweat it! Just post it whenever.
main cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//
//  main.cpp
//  ai
//
//  Created by Home on 4/3/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include <iostream>
#include "shipai.h"
#include "player.h" 

int main(){
    ai boss; 
    ship::player one; 
    char c; 
    int row, collumn = 7, aship = 0, quess = 0, pship = 0; 
    bool sunk = false; 
    // [row] [collumn] 
    char player[6][6] = {};
//places ship
    one.placeship(); 
    //all user input happens here
    while(!sunk){
        //prints the two arrays
        std::cout << "\nComputer:\n";
        one.printfield(player); 
        std::cout << '\n' << "Player:\n"; 
        one.printplayer(); 
        //player input for target location
        std::cout << "enter in your location!(q to exit)";
        std::cin >> c >> collumn;
        c = tolower(c); 
        //accounts for q
        if(c == 'q') sunk = true; 
        //converts the char into a number
        row = one.charcovert(c); 
        //counts number of guesses
        ++quess; 
        //place player move on grid
        if(boss.check(row, collumn)){
            player[--collumn][--row] = 'H';
            aship++; 
        }
        else {
            player[--collumn][--row] = 'M'; 
        }
        //place AI move on grid
        if(one.check(boss.passive1(),boss.passive2())) ++pship;
        // evaluates for ending loop
        if(aship == 3 || pship == 3) sunk = true; 
    }
    //output after game is over, needs work. 
    one.printfield(player); 
    std::cout << "YOU HAVE WON!!!! it took you " << quess << " tries";
    return 0;
    }

player interactions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//
//  player.h
//  ai
//
//  Created by Home on 4/3/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#ifndef ai_player_h
#define ai_player_h

#include <iostream>
namespace ship{
class player{
private:
    char playership[6][6], c; 
    int row, collumn; 
    
public: 
    player();
    
    void placeship(){
        std::cout << "enter the row[number] you would like to place your ship inside: ";
        std::cin >> row;
        std::cout << "enter the collumn[letter] you would like to place your ship in: ";
        std::cin >> c;
        collumn = charcovert(c); 
        playership[--row][--collumn] = 'S'; 
        if(row >= 3){
            playership[--row][collumn] = 'S';
            playership[--row][collumn] = 'S';
        }
        else{
            playership[++row][collumn] = 'S';
            playership[++row][collumn] = 'S'; 
        }
    }
    
    void printplayer(){
        printfield(playership);
    }
    
    bool check(int i, int j){
        if(playership[i][j] == 'S'){
            playership[i][j] = 'H';
            return true;
        }
        else{
            playership[i][j] = 'M'; 
            return false; 
        }
    }
    
    void printfield(char array[6][6]){
        std::cout << "\n  A B C D E F\n";
        for(int i = 0; i < 6; i++){
            std::cout << i + 1 << " "; printlane(i, array); 
            std::cout << '\n'; 
        }
    }
    
    void printlane(int i, char array[6][6]){
        for(int j = 0; j < 6; j++){
            std::cout << array[i][j] << " ";
        }
    }
    
    int charcovert(char c){ 
        if(c == 'a') return 1;
        if(c == 'b') return 2;
        if(c == 'c') return 3; 
        if(c == 'd') return 4;
        if(c == 'e') return 5;
        if(c == 'f') return 6;
        if(c == 'q') return 0; 
        else return 7; 
    }
    
    void printinfo(){
        std::cout << "********************";
        std::cout << "\n*                  *";
        std::cout << "\n*     BATTLESUB!   *";
        std::cout << "\n*     by me!       *"; 
        std::cout << "\n*                  *";
        std::cout << "\n********************"; 
    }
    
    
};

player::player(){
    for(int i = 0; i < 6; i++){
        for(int j = 0; j < 6; j++){
            playership[i][j] = 'O'; 
        }
    }
}
}

#endif 

computer interactions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//
//  AI.h
//  battleship
//
//  Created by Home on 4/2/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#ifndef battleship_AI_h
#define battleship_AI_h

#include <iostream>
#include <math.h>
#include <exception>
#include <fstream>

class ai {
private:
    int row, collumn, returncordiate[2];
    //row, collumn
    char aimap[6][6]; 
    
    
public:
    
    ai(); 
    
    bool check(int i, int j){
        if(aimap[i][j] == 'S') return true;
        else return false; 
    }
    
    int passive1(){
        srand(time(0));
        returncordiate[0] = rand()%7;
        return returncordiate[0]; 
    }
    
    int passive2(){
        srand(time(0));
        returncordiate[1] = rand()%7; 
        return returncordiate[1];
    }
    
};

ai::ai(){
    std::ifstream ship_location;
    ship_location.exceptions(std::ifstream::failbit | std::ifstream::badbit);
    //open file
    try{
        ship_location.open("/Users/home/Desktop/battleship/battleship/ship.txt");
    }
    //catch error
    catch (std::ofstream::failure &e){
        std::cout << "An error has occured";
        ship_location.close(); 
    }
    //upload array
    for(int j = 0; j < 6; j++){
        for(int i = 0; i < 6; i++){
            ship_location >> aimap[j][i]; 
        }
    }
    ship_location.close(); 
}

#endif

Thank you so much! I can't thank you enough! This will help me so much! Ahhhhhhhh!!! THANK YOU!!!!!!!!!
Topic archived. No new replies allowed.