Apr 1, 2013 at 10:10pm UTC
Hi I have a tic-tac-toe project for my basic c++ class. I am at a good start but I don't know how to code a win,tie, or a loss in it. here is my current code. Right now I am using a while loop so it plays infinitely since none of the spaces will equal 7. It is in the player actions function.
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
#include <iostream>
#include <iomanip>
using namespace std;
int const rcnum = 3;
char board[rcnum][rcnum];
int inst() // instructions
{
cout << "To play this game of tic-tac-toe on your turn enter first the \n" <<
"column number then enter the row number. Please do not enter a location\n" <<
" that is already in use if you do it will prompt for another move.\n" ;
}
int boards() // initialize and display board
{
int c;
for (c = 0; c <rcnum; c++)
{
board[c][0] = '*' ;
board [c][1] = '*' ;
board [c][2] = '*' ;
}
cout << " Columns \n" << setw(9)
<< " 1 " << "2 " << "3 \n" ;
for (c = 0; c <rcnum; c++)
{
cout << "row " << c + 1 << ": " << board[c][0]
<< " " << board [c][1] << " "
<< board [c][2] << " " << endl;
}
}
int act() // player actions
{
int row;
int column;
int c;
while (board[0][0] != 7) ///
{
cout << "Enter row number: " ;
cin >> row;
cout << "Enter column number: " ;
cin >> column;
column = column - 1;
row = row - 1;
while (board[row][column] == 'x' || board[row][column] == 'O' || row >= 3 || column >= 3)
{ cout << "Invalid move please enter again.\n" ;
cout << "Enter row number: " ;
cin >> row;
cout << "Enter column number: " ;
cin >> column;
column = column - 1;
row = row - 1; }
board[row][column] = 'x' ;
for (c = 0; c <rcnum; c++)
{ cout << "row " << c + 1 << ": " << board[c][0]
<< " " << board [c][1] << " "
<< board [c][2] << " " << endl; }
cout << "Enter row number: " ;
cin >> row;
cout << "Enter column number: " ;
cin >> column;
column = column - 1;
row = row - 1;
while (board[row][column] == 'x' || board[row][column] == 'O' || row >= 3 || column >= 3)
{ cout << "Invalid move please enter again.\n" ;
cout << "Enter row number: " ;
cin >> row;
cout << "Enter column number: " ;
cin >> column;
column = column - 1;
row = row - 1; }
board[row][column] = 'O' ;
for (c = 0; c <rcnum; c++)
{ cout << "row " << c + 1 << ": " << board[c][0]
<< " " << board [c][1] << " "
<< board [c][2] << " " << endl; }
}
}
int main()
{
inst();
boards();
act();
system("PAUSE" );
}
Last edited on Apr 1, 2013 at 10:13pm UTC
Apr 1, 2013 at 11:31pm UTC
It's alright any advice is good advice I'll edit that into my program.Can you explain why that is better though? I'm still trying to figure it out but no luck so far except maybe at the end I could manually put in a loop that checks for the winning moves but that would take a long time and I know I would get points taken off for not having more efficient coding.
Last edited on Apr 1, 2013 at 11:32pm UTC
Apr 2, 2013 at 12:00am UTC
uzferry, read the "Console closing down" thread before posting that again :)
cin.ignore(numeric_limits<streamsize>::max(), '\n' );
http://www.cplusplus.com/forum/beginner/1988/
@persontothe
How, in real life, would you know when there was a winner or a cat? See if you can convert that logic to code ;)
Last edited on Apr 2, 2013 at 12:01am UTC