Sep 25, 2013 at 12:48pm UTC
#include <iostream>
using namespace std;
int main()
{
//create the board
char board[9];
//initialize the board
int i=0;
while(i<9)
board[i++]=' ';
int tnumber=1;
char sym[2]={'0','X'};
while(tnumber<=9)
{
//print the board
i=0;
cout<<" "<<char(45)<<" "<<char(45)<<" "<<char(45)<<" "<<"\n";
cout<<"| ";
while(i<9){
if(i==3 || i==6){
cout<<"\n";
cout<<" "<<char(45)<<" "<<char(45)<<" "<<char(45)<<" "<<" ";
cout<<"\n| ";
}
cout<<board[i]<<" | ";
i++;
}
cout<<"\n";
cout<<" "<<char(45)<<" "<<char(45)<<" "<<char(45)<<" "<<"\n";
//take input
int row, col;
cout<<"Enter row number : "<<endl;
cin>>row;
cout<<"Enter column number : "<<endl;
cin>>col;
row-- ;
col-- ;
board[3*row+col]=sym[tnumber%2];
tnumber++;
}
return 0;
}
Sep 25, 2013 at 12:51pm UTC
plz tell me what should i do for wining and draw condition.................when i enter 1 row and 1 column then box 1 filled with x and when i again enter row1 andcolumn 1 then x replaced by 0 whyyyyyyyyy?????????
Sep 25, 2013 at 12:59pm UTC
Because by the looks of it you don't have code to validate whether a board position is occupied or not.
Sep 25, 2013 at 1:28pm UTC
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
#include <iostream>
using namespace std;
int main()
{
//create the board
char board[9];
//initialize the board
int i=0;
while (i<9)
board[i++]=' ' ;
int tnumber=1;
char sym[2]={'0' ,'X' };
while (tnumber<=9)
{
//print the board
i=0;
cout<<" " <<char (45)<<" " <<char (45)<<" " <<char (45)<<" " <<"\n" ;
cout<<"| " ;
while (i<9){
if (i==3 || i==6){
cout<<"\n" ;
cout<<" " <<char (45)<<" " <<char (45)<<" " <<char (45)<<" " <<" " ;
cout<<"\n| " ;
}
cout<<board[i]<<" | " ;
i++;
}
cout<<"\n" ;
cout<<" " <<char (45)<<" " <<char (45)<<" " <<char (45)<<" " <<"\n" ;
//take input
int row, col;
cout<<"Enter row number : " <<endl;
cin>>row;
cout<<"Enter column number : " <<endl;
cin>>col;
row-- ;
col-- ;
while ( board[3*row+col]=='X' || board[3*row+col]=='0' )
{
cout<<" enter row and column again " <<endl;
cin>>row>>col;
}
board[3*row+col]=sym[tnumber%2];
tnumber++;
}
int win1,win2;
if (board[1]=='X' && board[2]=='X' && board[3]=='X' )
{
win1;
tnumber=10;
}
if (board[4]=='X' && board[5]=='X' && board[6]=='X' )
{
win1;
tnumber=10;
}
if (board[7]=='X' && board[8]=='X' && board[9]=='X' )
{
win1;
tnumber=10;
}
if (board[1]=='X' && board[4]=='X' && board[7]=='X' )
{
win1;
tnumber=10;
}
if (board[2]=='X' && board[5]=='X' && board[8]=='X' )
{
win1;
tnumber=10;
}
if (board[3]=='X' && board[6]=='X' && board[9]=='X' )
{
win1;
tnumber=10;
}
if (board[1]=='X' && board[5]=='X' && board[9]=='X' )
{
win1;
tnumber=10;
}
if (board[3]=='X' && board[5]=='X' && board[7]=='X' )
{
win1;
tnumber=10;
}
if (board[1]=='0' && board[2]=='0' && board[3]=='0' )
{
win2;
tnumber=10;
}
if (board[4]=='0' && board[5]=='0' && board[6]=='0' )
{
win2;
tnumber=10;
}
if (board[7]=='0' && board[8]=='0' && board[9]=='0' )
{
win2;
tnumber=10;
}
if (board[1]=='0' && board[4]=='0' && board[7]=='0' )
{
win2;
tnumber=10;
}
if (board[2]=='0' && board[5]=='0' && board[8]=='0' )
{
win2;
tnumber=10;
}
if (board[3]=='0' && board[6]=='0' && board[9]=='0' )
{
win2;
tnumber=10;
}
if (board[1]=='0' && board[5]=='0' && board[9]=='0' )
{
win2;
tnumber=10;
}
if (board[3]=='0' && board[5]=='0' && board[7]=='0' )
{
win2;
tnumber=10;
}
if (win==1)
{
cout<<"player 1 is winner" <<endl;
}
if (win==2)
{
cout<<"player 2 is winner" <<endl;
}else
{
cout<<"game is draw" <<endl;
}
return 0;
}
Last edited on Sep 25, 2013 at 1:48pm UTC
Sep 25, 2013 at 1:30pm UTC
i applied win draw conditions but program does not display win conditions.....why??????????????????????????????????????????????????????????????????????????????????????????????
Sep 25, 2013 at 1:38pm UTC
Please use code tags when posting code, so that it's easier to read.
Sep 25, 2013 at 1:49pm UTC
MikeBoy......done...now check code
Sep 25, 2013 at 1:58pm UTC
A very simple way of checking (maybe) is to have a second 9-element array (maybe bool) that keeps track of all the occupied squares. then you could check this before adding another piece, or just check the current array for a 1 or 0 in it.
edit:
i applied win draw conditions but program does not display win conditions.....why
your code doesn't even compile for me.
Last edited on Sep 25, 2013 at 2:01pm UTC
Sep 25, 2013 at 2:14pm UTC
plz edit my program and gave right programm...plz plz....or tell me my fault
Sep 25, 2013 at 2:29pm UTC
yar..........................................help plz