shortening code

so i made this simple tictactoe game and it runs nicely but i feel like the code is too long especially the part for checking a straight line,, please give advice on shortening this code!

This is only a file that defines the class so dun wry wen you dont see a main

#include <iostream>
#include "Header.h"
using namespace std;

TicTacToe::TicTacToe()
{
for(int i = 0;i < 3;i++)
{
for(int n = 0;n < 3; n++)
{
board[i][n] = 0;
}
}
}//constructor for class TicTacToe

void TicTacToe::setBoard()
{
for(int i = 0;i < 3;i++)
{
for(int n = 0;n < 3; n++)
{
board[i][n] = 2;
}
}


}

void TicTacToe::displayBoard()
{
for(int i = 0;i < 3;i++)
{
for(int n = 0;n < 3; n++)
{
cout<<board[i][n]<<" "<<"|";
}
if(i < 2)
cout<<endl<<"---------"<<endl;
}

}

void TicTacToe::makeMove()
{
string command;
while (1)
{
cout<<endl<<"please make a move player1: ";
cin>>command;
if(command == "exit")
{
break;
}
else if((command.substr(0, 1) >= "1" && command.substr(0, 1) <= "8" && command.substr(1, 1) >= "1" && command.substr(1, 1) <= "8"))
{

int a,b;

a=command[0]-'1';
b=command[1]-'1';


cout<<a<<b<<endl;



board[a][b]=0;
}
displayBoard();
checkWin();
cout<<endl<<"please make a move player2: ";
cin>>command;
if(command == "exit")
{
break;
}
else if((command.substr(0, 1) >= "1" && command.substr(0, 1) <= "8" && command.substr(1, 1) >= "1" && command.substr(1, 1) <= "8"))
{

int a,b;

a=command[0]-'1';
b=command[1]-'1';


cout<<a<<b<<endl;


if(board[a][b] == 2)
{
board[a][b]=1;
}
else {
cout<<endl<<"please make a move player2: ";
cin>>command;
}
}
displayBoard();
checkWin();

}
}

void TicTacToe::checkWin()
{
string Player1status[] = {};
string Player2status[] = {};
for(int i = 0;i < 3;i++)
{
for(int n = 0;n < 3; n++)
{
if((board[i][n] == 0 && board[i][n-1] == 0 && board[i][n-2] == 0)||(board[i][n] == 0 && board[i][n-1] == 0 && board[i][n+1] == 0) || (board[i][n] == 0 && board[i][n+1] == 0 && board[i][n+2] == 0)||(board[i][n] == 0 && board[i+1][n+1] == 0 && board[i+2][n+2] == 0))
{
Player1status[1] = "win";
}
else if((board[i][n] == 0 && board[i-1][n] == 0 && board[i-2][n] == 0)||(board[i][n] == 0 && board[i-1][n] == 0 && board[i+1][n] == 0) || (board[i][n] == 0 && board[i+1][n] == 0 && board[i+2][n] == 0) || (board[i][n] == 0 && board[i-1][n+1] == 0 && board[i-2][n+2] == 0))
{
Player1status[1] = "win";
}
}

}

for(int i = 3;i > 0;i--)
{
for(int n = 3;n > 0;n--)
{
if((board[i][n] == 1 && board[i][n-1] == 1 && board[i][n-2] == 1)||(board[i][n] == 1 && board[i][n-1] == 1 && board[i][n+1] == 1) || (board[i][n] == 1 && board[i][n+1] == 1 && board[i][n+2] == 1)||(board[i][n] == 1 && board[i+1][n+1] == 1 && board[i+2][n+2] == 1))
{
Player2status[1] = "win";
}
else if((board[i][n] == 1 && board[i-1][n] == 1 && board[i-2][n] == 1)||(board[i][n] == 1 && board[i-1][n] == 1 && board[i+1][n] == 1) || (board[i][n] == 1 && board[i+1][n] == 1 && board[i+2][n] == 1) || (board[i][n] == 1 && board[i-1][n+1] == 1 && board[i-2][n+2] == 1))
{
Player2status[1] = "win";
}
}

}

if(Player1status[1] == "win")
{
cout<<endl<<"player1 wins";
}
if(Player2status[1] == "win")
{
cout<<endl<<"player2 wins";
}

}
When pasting code, use the formatters <> next to the text box.
1
2
if (x>10)
  x--; //this code looks readable. 
For check win, just have it return 0 (which is equivalent to false) when no one wins, else have it return the winning player's token (which is equivalent to true if it's not 0 as far as the compiler is concerned).
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
int TicTacToe::checkWin()
{	
  int slot;
  //Check for the six possible horizontal and vertical wins
  for(int i=0; i<3; i++)
  {
    slot = board[i][0];
    if(board[i][1] == slot && slot != EMPTY) // I'm not sure what integer you use for an empty space.
      if(board[i][2] == slot)
        return slot; //return winning player's token.
            
    slot = board[0][i];
    if(board[1][i] == slot && slot != EMPTY)
      if(board[2][i] == slot)
        return slot; //return winning player's token.
  }
  //check diagonals
  slot = board[0][0];
  if(board[1][1] == slot && slot != EMPTY)
    if(board[2][2] == slot)
      return slot;
  slot = board[0][2];
  if(board[1][1] == slot && slot != EMPTY)
    if(board[2][0] == slot)
      return slot;
  return 0; //No one has won yet. return 0.
}
Why don't you make checkWin*() generic so it checks if a single player won?
Pass in the player number as an argument.
thx !! great help
Topic archived. No new replies allowed.