#include #include #include "TTTGame.h" #include "TTT.h" using namespace std; /* prototypes for the User Interface functions */ bool commandLineUI (int); void showBoard (); void gameOver (int); void showStats (); void showAbout (); void showCommands (); void showHelp (); /* * Class: TTT * Method: cmain * Signature: ()I */ JNIEXPORT jint JNICALL Java_TTT_cmain (JNIEnv * jenv, jobject job) { /* create the instance of the Tic-Tac-Toe Game */ TTTGame *game = TTTGame::createTTTGame(); /* start the first game */ cout << "Welcome to Tic-Tac-Toe"; if (game->newGame () == USERPLAYER) cout << ", you go first." << endl; else cout << ", the computer goes first." << endl; /* Call the user interface function */ commandLineUI ( 1); return 4; } bool commandLineUI ( int p ) { bool cont = true; char input [100]; char command, junk; int i; int j; int moveValue; /* access the instance of the Tic-Tac-Toe Game */ TTTGame *game = TTTGame::getGame(); /* loop until user enters command to quit */ while (cont) { /* check for computer player's move */ if (game->getTurn() == COMPUTERPLAYER) { moveValue = game->computerMove(); if (moveValue > VALIDMOVE) { gameOver(moveValue); continue; } } /* print current board */ showBoard(); cout << "Enter Command (1-9|n|s|h|?|a|q): "; /* read until the first non-white space character */ command = cin.get(); while (isspace(command)) command = cin.get(); /* read the remaining characters until the end of line */ junk = cin.get(); while (junk != '\n') junk = cin.get(); tolower(command); if (isdigit(command)) { /* User entered a move */ moveValue = game->userMove(command); if (moveValue > VALIDMOVE) gameOver(moveValue); else if (moveValue == INVALIDMOVE) cout << "Command \"" << command << "\" is not valid." << endl; else if (moveValue == POSITIONINUSE) cout << "Position " << command << " has already been picked." << " Select another position." << endl; } else if (command == 's') { showStats(); } else if (command == 'a') { showAbout(); } else if (command == 'h') { showHelp(); } else if (command == '?') { showCommands(); } else if (command == 'n') { cout << "Starting new Game"; if (game->newGame () == USERPLAYER) cout << ", you go first." << endl; else cout << ", the computer goes first." << endl; } else if (command == 'q') { showStats(); cout << "Quitting Program. Goodbye" << endl; cont = false; } else cout << "Command \"" << command << "\" is not valid." << endl; } return true; } void showStats() { /* access the instance of the Tic-Tac-Toe Game */ TTTGame *game = TTTGame::getGame(); cout << "There have been " << game->getNumStarted() << " game(s) started.\nYou have won " << game->getNumWon() << " game(s).\nYou have lost " << game->getNumLost() << " games(s).\nThere have been " << game->getNumCats() << " \"cats\" game(s) (no winner or loser)." << endl; } void showBoard() { /* access the instance of the Tic-Tac-Toe Game */ TTTGame *game = TTTGame::getGame(); int j; /* print current board */ for (j=1; j<=9; ++j) { cout << " " << game->getPosition(j) << " "; if ((j == 3) || (j == 6)) cout << "\n---|---|---\n"; else if (j == 9) cout << endl; else cout << "|"; } } void gameOver (int status) { cout << endl; if (status == WINGAME) cout << "You won the game!\n"; else if (status == LOSEGAME) cout << "You lost the game!\n"; else cout << "\"Cats\" game(no winner or loser)\n"; showBoard(); /* access the instance of the Tic-Tac-Toe Game */ TTTGame *game = TTTGame::getGame(); cout << "\nStarting a new Game"; if (game->newGame () == USERPLAYER) cout << ", you go first." << endl; else cout << ", the computer goes first." << endl; } void showAbout () { cout << "Tic-Tac-Toe Game\n\nWritten by Pat Troy" << "\nFebruary 2004 for CS 422" << endl; } void showCommands () { cout << "The commands for Tic-Tac-Toe are:\n" << " 1-9: Place an \'X\' in the corresponding\n" << "\tlocation on the game grid\n" << " n: Start a New Game\n" << " s: Show Statistics about the games played\n" << " h: Show Information on how to play\n" << " ?: Show the list of commands\n" << " q: Quit the game\n" << " a: Display information about the program" << endl; } void showHelp () { cout << "The game Tic-Tac-Toe is played on a 3x3 grid.\n" << "The goal of the game is to mark 3 positions in\n" << "a row on the grid before your opponent marks 3\n" << "positions on the grid. The positions can be either\n" << "horizontal, vertical or diagonal.\n\n" << "You will mark your positions with an \'X\' while\n" << "your computer opponent will mark its positions with\n" << "an \'O\'. The player that goes first will alternate\n" << "with the start of each new game." << endl; }