Hack Forums
[C] Just made my first project in C++ (tic tac toe) - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C] Just made my first project in C++ (tic tac toe) (/showthread.php?tid=6097872)



Just made my first project in C++ (tic tac toe) - XxClassifiedxX - Jul 26, 2020

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;
}



RE: Just made my first project in C++ (tic tac toe) - saltycracker - Jul 27, 2020

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