Aug 10, 2012 at 6:47am UTC
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";
}
}
Aug 10, 2012 at 1:54pm UTC
Why don't you make checkWin*() generic so it checks if a single player won?
Pass in the player number as an argument.