I just got finished with my first program (that is, the first one I haven't just copied out of a book). It's a text based version of Noughts and Crosses (aka Tic-tac-toe) without an AI, and it looks pretty ugly to me. Commenting is half-assed, some variables are given nondescript names, and I can tell that my loop structure can use a little work. Still, it runs.
Show me up, /prog/. Write a more elegant version in the language of your choice. Surely it's not a difficult task for [o][u]EXPERT PROGRAMMERS[/o][/u] such as yourselves. If you've got any suggestions for me, those would be nice, too.
//Top-left to bottom-right diagonal checker function of checkboard
int diagcheck1 (char x[9], char y, int z) {
if (x[z] == y) {
if (x[z+4] == y) {
if (x[z+8] == y)
return 1;
//Top-right to bottom-left diagonal checker function of checkboard
int diagcheck2 (char x[9],char y,int z) {
if (x[z] == y) {
if (x[z+2] == y) {
if (x[z+4] == y)
return 1;
//Checks board for a win condition
//Uses horicheck, verticheck, diagcheck1, and diagcheck2
int checkboard (char a[], char b) {
if (horicheck (a,b,0) == 1)
return 1;
else if (horicheck (a,b,3) == 1)
return 1;
else if (horicheck (a,b,6) == 1)
return 1;
else if (verticheck (a,b,0) == 1)
return 1;
else if (verticheck (a,b,3) == 1)
return 1;
else if (verticheck (a,b,6) == 1)
return 1;
else if (diagcheck1 (a,b,0) == 1)
return 1;
else if (diagcheck2 (a,b,2) == 1)
return 1;
else
return 0;
}
int main () {
char board[9] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
char curplayer = 'X';
int done;
char winner = 'q';
int turn;
int fin = 0;
cout << "Noughts and Crosses\n\n";
cout << " 0 | 1 | 2\n---+---+---\n 3 | 4 | 5\n---+---+---\n 6 | 7 | 8\n";
cout << "To place your nought or cross in a square, type the location of\nits square, as seen above.\n\n";
do {
cout << curplayer << "'s turn!\n";
dispboard (board);
done = 0;
while (done == 0) {
cout << "Player " << curplayer << ", please type the location of the square that\n";
cout << "you wish to place a '" << curplayer << "'this turn. ";
cin >> turn;
if (turn >= 0 && turn < 9) {
if (board[turn] == ' ') {
board[turn] = curplayer;
done = 1;
cout << "\n'" << curplayer << "' placed!\n";
}
else
cout << "\nThere is already a nought or cross at that location.\n";
}
else
cout << "\nYou did not specifiy a valid square. Locations range from 0 to 8,\nwith 0 being the upper left square and 8 being the bottom left.\n";
}
if (checkboard (board, 'X') == 1)
winner = 'X';
else if (checkboard (board, 'Y') == 1)
winner = 'O';
else {
if (curplayer == 'X')
curplayer = 'O';
else
curplayer = 'X';
}
} while (winner == 'q');