Please help URGENT C++
Aug 6, 2014 at 2:29pm UTC
I am creating a Tic Tac Toe game through coding but would like the grid to be 4x4 or 5x5 to make it more complicated. I have no idea how to do it, this is the code i have sourced from
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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
#include <iostream>
#include <string.h>
#include <sstream>
#include <ctype.h>
using namespace std;
class game{
private :
int place,gameOn,turn,winner;
char board[9];
unsigned short int win[9];
string posZero,posOne,posTwo,posThree, posFour,posFive, posSix,posSeven, posEight;
public :
game()
{
turn = 0;
for (int i = 0; i < 9; i++)
{
board[i] = '\0' ;
win[i] = '\0' ;
}
cout << board[1];
}
int getWinner()
{ return winner; }
void setTurn()
{ turn = (turn == 0) ? 1 : 0; }
void setCords()
{
int x = 0;
cout << "\n\nEnter place number: " ;
cin >> x;
cout << "\n" ;
place = x - 1;
}
int setGrid()
{
if (board[place] == 'O' || board[place] == 'X' )
{
cout << "\nThat place is already taken!" << endl;
draw();
return 0;
}
else if (win[place] == 1 || win[place] == 2)
{
cout << "\nWin already taken\n" ;
draw();
return 0;
}
else
{
if (turn == 0)
{
board[place] = 'X' ;
win[place] = 1;
return 1;
}
else
{
board[place] = 'O' ;
win[place] = 2;
return 1;
} } }
bool play()
{
for (int x = 1; x < 3; x++)
{
if (win[0] == x)
{
if (win[1] == x) { if (win[2] == x) { winner = x; return true ; } }
if (win[4] == x) { if (win[8] == x) { winner = x; return true ; } }
if (win[3] == x) { if (win[6] == x) { winner = x; return true ; } }
}
if (win[1] == x)
{ if (win[4] == x) { if (win[7] == x) { winner = x; return true ; } } }
if (win[2] == x)
{
if (win[5] == x) { if (win[8] == x) { winner = x; return true ; } }
if (win[4] == x) { if (win[6] == x) { winner = x; return true ; } }
}
if (win[3] == x)
{ if (win[4] == x) { if (win[5] == x) { winner = x; return true ; } } }
if (win[6] == x)
{ if (win[7] == x) { if (win[8] == x) { winner = x; return true ; } } }
}
return false ;
}
void draw()
{ cout << endl << posZero <<posOne << posTwo << posThree << posFour << posFive << posSix << posSeven << posEight; }
void drawChange()
{
for (int i = 0; i < 9; i++)
{
switch (i)
{
case 0:
if (board[0] == 'X' )
{ posZero = "_X_|" ; }
else if (board[0] == 'O' )
{ posZero = "_O_|" ; }
else
{ posZero = "___|" ; }
break ;
case 1:
if (board[1] == 'X' )
{ posOne = "_X_|" ; }
else if (board[1] == 'O' )
{ posOne = "_O_|" ; }
else
{ posOne = "___|" ; }
break ;
case 2:
if (board[2] == 'X' )
{ posTwo = "_X_" ; }
else if (board[2] == 'O' )
{ posTwo = "_O_" ; }
else
{ posTwo = "___" ; }
break ;
case 3:
if (board[3] == 'X' )
{ posThree = "\n_X_|" ; }
else if (board[3] == 'O' )
{ posThree = "\n_O_|" ; }
else
{ posThree = "\n___|" ; }
break ;
case 4:
if (board[4] == 'X' )
{ posFour = "_X_|" ; }
else if (board[4] == 'O' )
{ posFour = "_O_|" ; }
else
{ posFour = "___|" ; }
break ;
case 5:
if (board[5] == 'X' )
{ posFive = "_X_" ; }
else if (board[5] == 'O' )
{ posFive = "_O_" ; }
else
{ posFive = "___" ; }
break ;
case 6:
if (board[6] == 'X' )
{ posSix = "\n X |" ; }
else if (board[6] == 'O' )
{ posSix = "\n O |" ; }
else
{ posSix = "\n |" ; }
break ;
case 7:
if (board[7] == 'X' )
{ posSeven = " X |" ; }
else if (board[7] == 'O' )
{ posSeven = " O |" ; }
else
{ posSeven = " |" ; }
break ;
case 8:
if (board[8] == 'X' )
{ posEight = " X " ; }
else if (board[8] == 'O' )
{ posEight = " O " ; }
else
{ posEight = " " ; }
break ;
}
}
cout << posZero <<posOne << posTwo << posThree << posFour << posFive << posSix << posSeven << posEight; }
};
int main()
{ for (;;){
game TTT;
while (!TTT.play())
{
TTT.drawChange();
int r = 0;
do {
TTT.setCords();
r = TTT.setGrid();
}while (r < 1);
TTT.setTurn();
}
TTT.drawChange();
if (TTT.getWinner() == 1)
{ cout << "\nThe Winner is X. Congrats" << endl; }
if (TTT.getWinner() == 2)
{ cout << "\nThe winner is O. Congrats" << endl; }
}
return 0;
}
Aug 6, 2014 at 2:34pm UTC
Aug 9, 2014 at 7:25pm UTC
There is already a post on this, please don't double post. Anyways, this thread should be closed.
Topic archived. No new replies allowed.