...Wouldn't you just use a 3x3 array? And then there are only... one two three four five six seven eight.. eight different checks to make for whether or not someone wins. Which you can narrow by checking where they placed their last piece (IE, bottom left would have left column right column diagonal checks). But that'd require more effort. (1:29 AM now)
Crappy code just seeing how fast I can write it.
Code: Select all
//Open-source tic-tac-toe by Alex Wilson (MechMarauderIIC@hotmail.com)
//Feel free to distribute and modify without giving any credit to me at all whatsoever.
//Dedicated to JSLemming's keyboard ;)
#include <iostream>
#include <ctype.h>
#include <string>
char board[3][3] = {
'n', 'n', 'n',
'n', 'n', 'n',
'n', 'n', 'n'
};
void displayBoard();
bool doChecks(char checking);
using namespace std;
int main() {
string userInput = " ";
int player = 1;
int x, y = -1;
cout << "please note that the coordinates are zero based." << endl;
displayBoard();
do {
cout << "player: " << player << ": x-coordinate? ";
getline(cin, userInput);
if (!(isdigit(userInput[0]))) {
continue;
}
x = atoi(userInput.c_str());
cout << endl << "player: " << player << ": y-coordinate? ";
getline(cin, userInput);
if (!(isdigit(userInput[0]))) {
cout << "not a digit" << endl;
x = y = -1;
continue;
}
y = atoi(userInput.c_str());
if (y < 0 || x < 0 || y > 2 || x > 2) {
cout << "invalid" << endl;
x = y = -1;
continue;
}
if (board[x][y] != 'n') {
cout << "spot taken" << endl;
x = y = -1;
continue;
}
if (player == 1) {
board[x][y] = 'x';
} else if (player == 2) {
board[x][y] = 'o';
}
displayBoard();
if (player == 1) {
if (doChecks('x')) {
cout << "p1 == WINNER!!!" << endl;
break;
}
player = 2;
} else {
if (doChecks('o')) {
cout << "p2 == WINNER!!" << endl;
break;
}
player = 1;
}
} while (userInput[0] != 'q');
cout << "done - hit enter to quit" << endl;
getline(cin, userInput);
return 0;
}
void displayBoard() {
cout << endl << endl << endl << endl;
cout << " 012" << endl;
for (unsigned int ycount = 0;ycount < 3;ycount++) {
cout << ycount;
for (unsigned int xcount = 0;xcount < 3;xcount++) {
cout << board[xcount][ycount];
}
cout << endl;
}
}
bool doChecks(char checking) {
//cheap cruddy checks
int soFar = 0;
unsigned int xcheck, ycheck;
//vert checks
for (xcheck = 0;xcheck < 3;xcheck++) {
for (ycheck = 0;ycheck < 3;ycheck++) {
if (board[xcheck][ycheck] != checking) {
soFar = 0;
break;
}
soFar++;
if (soFar == 3)
return true;
}
soFar = 0;
}
//horiz checks
for (ycheck = 0;ycheck < 3;ycheck++) {
for (xcheck = 0;xcheck < 3;xcheck++) {
if (board[xcheck][ycheck] != checking) {
soFar = 0;
break;
}
soFar++;
if (soFar == 3)
return true;
}
soFar = 0;
}
//diagonal check
if (board[1][1] != checking)
return false;
if ((board[0][0] == checking && board[2][2] == checking) ||
(board[0][2] == checking && board[2][0] == checking))
{
return true;
}
return false;
}
it is now 1:49 AM checking to see if it compiles :)
1:53 am - it does after a few parentheses but doesn't work yet ;)
1:55 am - runs. testing to see if it works
1:57 am - doh, has x and y coords flipped.
1:59 am - fixed the x and y coords flipped (was in display)
2:01 am - fixed the display (again ;) ) - counter doesn't work right yet
2:05 am - okay, i think it works 100%. time for the "extensive playtesting" phase.
2:19 am - fixed p2 winning (was checking for 'y' instead of 'o'. yay for mornings :P) - tested and vert wins on a couple places each and both diagonal wins. Pretty sure it all works. Oh. And made it use tabs instead of spaces, since I originally wrote it on the forum :)
Sorry. Just wanted to see if I could do it (quickly). Hadn't written a tic-tac-toe program yet :D
So 20 mins for original code layout. Then 30 minutes of fixing/code-pretty. ;) Total time: ~45 minutes if we allow for me replacing spaces with tabs and doing little things like making it quit when you win and outputting the x-y coordinates. ... yay.
(2:21-2:29 AM - made it prettier. no coding changes, just spaces, a few comments, broke the diagonalchecking line and put {}'s in, etc. edited this post a lot, talked to people, blah blah.
2:30 am- dedicated it to jslemming's keyboard
times are in EST)
Oh, and no I didn't have any instructions. Just thought about how it would work, and how people check to see if they win. Yay, I proved to myself that I can do it. So happy :))
3:25 am : hunted some crap about input. found a new function, std::getline(). then found out why it did not work correctly. fixed the problem. this now uses c++ strings for input. so much better! (read: don't have to check to see if the user wrote more than a character array can handle, don't have to trap the overflow, etc, etc. yes!! learned something new!! funny how
www.cppreference.com doesn't list the std::string::getline() function, or i would have learned about it sooner)
IMPORTANT REFERENCE FOR YOU (if you use msvc++ anyway):
http://support.microsoft.com/default.as ... US;q240015
(and in general:
http://www.bgsu.edu/departments/compsci ... tring.html )