Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

Just made my first project in C++ (tic tac toe)

This is my first time programming in c++, so don't roast me too hard lol. Anyways it took me about an hour, but here it is. If you guys do not mind could you give me some pointers on how to improve it. Thanks!

Code
# include <iostream>
using namespace std;

bool gameOver;

class Board{
   private:
       string board[3][3] = {{"_","_","_"},{"_","_","_"},{"_","_","_"}};
   public:
       void draw(){
           system("cls");
           cout << "The top left position is 0 then 1 then 2 all the way to 8" << endl;
           for(int row = 0; row < 3; row++){
               for(int column = 0; column < 3; column++){
                   cout << board[row][column] << " ";
               }
               cout << endl;
           }
       }

       bool checkWin(string playerChar){
           if(playerChar == board[0][0] && playerChar == board[0][1] && playerChar == board[0][2]){
               return true;
           }
           if(playerChar == board[1][0] && playerChar == board[1][1] && playerChar == board[1][2]){
               return true;
           }
           if(playerChar == board[2][0] && playerChar == board[2][1] && playerChar == board[2][2]){
               return true;
           }
           if(playerChar == board[0][0] && playerChar == board[1][0] && playerChar == board[2][0]){
               return true;
           }
           if(playerChar == board[0][1] && playerChar == board[1][1] && playerChar == board[2][1]){
               return true;
           }
           if(playerChar == board[0][2] && playerChar == board[1][2] && playerChar == board[2][2]){
               return true;
           }
           if(playerChar == board[0][0] && playerChar == board[1][1] && playerChar == board[2][2]){
               return true;
           }
           if(playerChar == board[0][2] && playerChar == board[1][1] && playerChar == board[2][0]){
               return true;
           }
       }

   void updateBoard(string playerChar, int row, int column){
       board[row][column] = playerChar;
       }
};



int main(){

   int row;
   int column;
   Board board;
   gameOver = false;

   while(!gameOver){

       board.draw();

       cout << "Player 1 row: ";
       cin >> row;
       cout << endl << "Player 1 column ";
       cin >> column;
       board.updateBoard("X", row, column);
       if(board.checkWin("X") == true){
           board.draw();
           cout << endl << "Player 1 wins!!!!!!!!!!!";
           gameOver = true;
           break;
       }

       board.draw();

       cout << "Player 2 row: ";
       cin >> row;
       cout << endl << "Player 2 column ";
       cin >> column;
       board.updateBoard("O", row, column);
       if(board.checkWin("O") == true){
           board.draw();
           cout << endl << "Player 1 wins!!!!!!!!!!!";
           gameOver = true;
           break;
       }
   }
   return 0;
}
You really should investigate C++'s vector class and constants.

You have a problem with this function
Code
bool checkWin(string playerChar){
           if(playerChar == board[0][0] && playerChar == board[0][1] && playerChar == board[0][2]){
               return true;
           }
.....
           if(playerChar == board[0][2] && playerChar == board[1][1] && playerChar == board[2][0]){
               return true;
           }
       }
control reaches end of non-void function
May you be given directions by software engineer document writers.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Java] by FinnTheHumanMar 27, 2017 01:43 PM 12 573 Mar 27, 2017 01:43 PM
Last Post: 3cli‌‌pse
[.NET] by XoggFeb 24, 2017 11:53 AM 8 268 Feb 24, 2017 11:53 AM
Last Post: Barret
[Java] by FinnTheHumanFeb 6, 2017 05:27 PM 0 162 Feb 6, 2017 05:27 PM
Last Post: FinnTheHuman