Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Program : Tic Tac Toe
- * Author : Michael Stroud
- * Version : 1.0
- * Date : 20141002
- * Purpose : A simple 2 player tic tac toe game for the command line.
- */
- #include <iostream>
- #include <limits>
- char gameboard[3][3] = {{'7','8','9'},
- {'4','5','6'},
- {'1','2','3'}};
- char playerSymbols[] = {'X','O'};
- void drawboard();
- void processPlayerMove(int);
- int getCoord();
- int xFromCoord(int coord);
- int yFromCoord(int coord);
- enum gamestate{
- UNFINISHED,
- XWINS,
- OWINS,
- DRAW
- };
- gamestate getGameState(int);
- int main(){
- std::cout << "***TIC TAC TOE***" << std::endl;
- drawboard();
- int elapsedTurns = 0;
- while (getGameState(elapsedTurns) == UNFINISHED){
- processPlayerMove(elapsedTurns%2);
- drawboard();
- ++elapsedTurns;
- }
- switch (getGameState(elapsedTurns)){
- case DRAW:
- std::cout << "Cat's game." << std::endl;
- break;
- case XWINS:
- std::cout << "Player 1 wins." << std::endl;
- break;
- case OWINS:
- std::cout << "Player 2 wins." << std::endl;
- break;
- }
- }
- void drawboard(){
- for (int i = 0; i < 3; ++i){
- for (int k = 0; k < 3; ++k){
- std::cout << " " << gameboard[i][k];
- if (k < 2){
- std::cout << " |";
- }
- }
- std::cout << std::endl;
- if (i < 2){
- std::cout << "---+---+---" << std::endl;
- }
- }
- }
- void processPlayerMove(int playerNumber){
- std::cout << "Player " << playerNumber+1 << " choose where to place \'" <<
- playerSymbols[playerNumber] << "\'" << std::endl;
- int targetCoord = getCoord();
- while (gameboard[yFromCoord(targetCoord)][xFromCoord(targetCoord)] != (targetCoord+'0')){
- std::cout << "That spot is occupied. Please choose another spot." << std::endl;
- targetCoord = getCoord();
- }
- gameboard[yFromCoord(targetCoord)][xFromCoord(targetCoord)] =playerSymbols[playerNumber];
- }
- int getCoord(){
- char userIn;
- do {
- std::cin >> userIn;
- }while (userIn < '0' || userIn > '9');
- std::cin.clear();
- std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
- return userIn-'0';
- }
- int yFromCoord(int coord){
- int zeroedCoord = coord-1;
- zeroedCoord = 8-zeroedCoord;
- return zeroedCoord/3;
- }
- int xFromCoord(int coord){
- int zeroedCoord = coord-1;
- return zeroedCoord%3;
- }
- gamestate getGameState(int elapsedTurns){
- if (elapsedTurns < 5){
- return UNFINISHED;
- }
- //check for vertical win
- for (int i = 0; i < 3; i++){
- if (gameboard[0][i] == gameboard[1][i] &&
- gameboard[0][i] == gameboard[2][i]){
- if (gameboard[0][i] == 'X'){
- return XWINS;
- }else{
- return OWINS;
- }
- }
- }
- //check for horizontal win
- for (int i = 0; i < 3; i++){
- if (gameboard[i][0] == gameboard[i][1] &&
- gameboard[i][0] == gameboard[1][2]){
- if (gameboard[i][0] == 'X'){
- return XWINS;
- }else{
- return OWINS;
- }
- }
- }
- //check for diagonal win
- if (gameboard[0][0] == gameboard[1][1] &&
- gameboard[0][0] == gameboard[2][2]){
- if (gameboard[0][0] == 'X'){
- return XWINS;
- }else{
- return OWINS;
- }
- }
- if (gameboard[2][0] == gameboard [1][1] &&
- gameboard[2][0] == gameboard[0][2]){
- if (gameboard[2][0] == 'X'){
- return XWINS;
- }else{
- return OWINS;
- }
- }
- if (elapsedTurns == 9){
- return DRAW;
- }
- return UNFINISHED;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement