The Code-Bin
Links
Home
Add your code!
All Listings
About
Latest Entry
Featured Scripts
Author's Website
Latest Entries
FFMPEG Thumbnail Scr...
PHP, 0.8KB
Jul. 29, 10:24pm
John
Z80 Assembler, 190 bytes
Feb. 17, 3:36am
John
Z80 Assembler, 176 bytes
Sep. 13, 2:19am
John
Z80 Assembler, 77 bytes
Sep. 13, 2:18am
John
Z80 Assembler, 209 bytes
Sep. 13, 2:17am
untitled C++ Code
Posted by: Matt | September 8, 2010 @ 11:11pm
C++ Code
[
Download
]
//Matthew Isnor 8 September 2010 #include <iostream> #include <windows.h> using namespace std; char board[9] = {' ',' ',' ',' ',' ',' ',' ',' ',' '}; void showBoard(){ cout << " " << board[0] << " | " << board[1] << " | " << board[2] << endl; cout << " _ " << " _ " << " _ " << endl; cout << " " << board[3] << " | " << board[4] << " | " << board[5] << endl; cout << " _ " << " _ " << " _ " << endl; cout << " " << board[6] << " | " << board[7] << " | " << board[8] << endl; } void move(char player){ int choice; bool input = 0; while (input == 0) { cout << "Please make a move selection 0-8: " << endl; cin >> choice; if (choice > 8 || choice < 0) { cout << "Please enter a move selection 0-8: " << endl; cin >> choice; } if (board[choice] == 'X' || board[choice] == 'O') { cout << "That spot is taken on the board. Choose another 0-8: " << endl; cin >> choice; } board[choice] = player; input = 1; } } char checkWinner(char player, char winner){ if (board[0] == player && board[1] == player && board[2] == player) // Across top { return player; } else if (board[0] == player && board[4] == player && board[8] == player) //Top left bottom right { return player; } else if (board[0] == player && board[3] == player && board[6] == player) // Left side { return player; } else if (board[1] == player && board[4] == player && board[7] == player) //Down middle { return player; } else if (board[2] == player && board[5] == player && board[8] == player) //right side { return player; } else if (board[6] == player && board[7] == player && board[8] == player) // bottom row { return player; } else if(board[3] == player && board[4] == player && board[5] == player) //middle accross { return player; } else if(board[6] == player && board[4] == player && board[2] == player) // bottom left to top right { return player; } else { return 'f'; // NO ONE FUCKING WON, KEEP GOING. } } void winMessage(char player){ system("CLS"); showBoard(); cout << "\n\nThe player '" << player << "' has won the match!" << endl; system("PAUSE"); } bool tieCheck(){ if ((board[0] == 'X' || board[0] == 'O') && (board[1] == 'X' || board[1] == 'O') && (board[2] == 'X' || board[2] == 'O') && (board[3] == 'X' || board[3] == 'O') && (board[4] == 'X' || board[4] == 'O') && (board[5] == 'X' || board[5] == 'O') && (board[6] == 'X' || board[6] == 'O') && (board[7] == 'X' || board[7] == 'O') && (board[8] == 'X' || board[8] == 'O')){ cout << "The match has ended in a tie!\n\n"; system("PAUSE"); return 1; } return 0; // Game did not end in a tie. } int main(){ bool winner = false; char theWinner = 'f'; cout << "Tic Tac Toe, C++ Style." << endl; while (winner == false) { system("CLS"); showBoard(); move('X'); theWinner = checkWinner('X', theWinner); if (theWinner == 'X') { winMessage('X'); return 0; // end the bitch 'X' won. } winner = tieCheck(); if (winner == 1) { return 0; // end the bitch 'O' won. } system("CLS"); showBoard(); move('O'); theWinner = checkWinner('O', theWinner); if (theWinner == 'O') { winMessage('O'); return 0; // end the bitch 'O' won. } winner = tieCheck(); if (winner == 1) { return 0; // end the bitch 'O' won. } // else re-loop while } }
Syntax Highlighting
[
Open in new window
]
Author Comments
tic tac toe
Rating
4.45 / 8
58 Votes