Originally posted by RoD
attach the whole cpp file.
I figured it out, but here is the attachments of the whole cpp file....warning...its still in development.
axon
Code:
/*
Program: Simple tic-tac-toe game
Date: 02/21/03
*/
#include <iostream>
#include <cctype>
#include <cstdlib>
using namespace std;
//declaring global variables for the positions on the board
char p1, p2, p3, p4, p5, p6, p7, p8, p9;
//function displaying the game instructions
void displayInstructions()
{
cout << "\tWelcome To TicTacToe!!\n\n"
<< "Play against another player or the computer,\n"
<< " by choosing from the menu below.\n\n"
<< "Enter positions as a letter and number.\n"
<< "\t ENJOY!!\n\n\n";
}
void displayMenu(int& menuOption)
{
cout << "Choose from the following options:\n"
<< " 1. Two Player Game\n"
<< " 2. Single Player as 'X'\n"
<< " 3. Single Player as 'O'\n"
<< "Selection: ";
cin >> menuOption;
if(menuOption == 1)
{cout << "\n\n You chose a Single Player Game!\n\n";}
else if(menuOption == 2)
{cout << "\n\n You chose to play against the computer as 'X'\n\n";}
else if(menuOption == 3)
{cout << "\n\n You chose to play against the computer as 'O'\n\n";}
}
//================================================================================
//function displaying the game board
void displayBoard()
{
cout << "\n\n"
<< "\t 1 2 3 \n"
<< "\t A "<<p1<<" | "<<p2<<" | "<<p3<<" \n"
<< "\t ----------- \n"
<< "\t B "<<p4<<" | "<<p5<<" | "<<p6<<" \n"
<< "\t ----------- \n"
<< "\t C "<<p7<<" | "<<p8<<" | "<<p9<<" \n\n";
cout << endl;
}
//================================================================================
//function switching the coordinates to the global vars
int switchCoordinatesToPosition(char letterPosition, int numberPosition, int&badMove)
{
if( (letterPosition == 'A') && (numberPosition == 1) )
{
if(p1 != ' ') {badMove = -1;}
else{ return 1; }
}
else if( (letterPosition == 'A') && (numberPosition == 2) )
{
if(p2 != ' ') {badMove = -1;}
else{ return 2; }
}
else if( (letterPosition == 'A') && (numberPosition == 3) )
{
if(p3 != ' ') {badMove = -1;}
else{ return 3; }
}
else if( (letterPosition == 'B') && (numberPosition == 1) )
{
if(p4 != ' ') {badMove = -1;}
else{ return 4; }
}
else if( (letterPosition == 'B') && (numberPosition == 2) )
{
if(p5 != ' ') { badMove = -1;}
else{ return 5; }
}
else if( (letterPosition == 'B') && (numberPosition == 3) )
{
if(p6 != ' ') { badMove = -1;}
else{ return 6; }
}
else if( (letterPosition == 'C') && (numberPosition == 1) )
{
if(p7 != ' ') {badMove = -1;}
else{ return 7; }
}
else if( (letterPosition == 'C') && (numberPosition == 2) )
{
if(p8 != ' ') { badMove = -1;}
else{ return 8; }
}
else if( (letterPosition == 'C') && (numberPosition == 3) )
{
if(p9 != ' ') { badMove = -1;}
else{ return 9; }
}
else { cout << "\nInvalid command...exiting!"; exit(-1);} //default escape
}
//=================================================================================
//function displaying the 'to move' prompt
void displayMovePrompt(char& playerValue, char& letterPosition, int& numberPosition,
int& positionNumberOnBoard, int badMove)
{
do
{
badMove=1;
switch(playerValue){
case 'X': cout << "\n\nPlayer X type in a move position: ";
break;
case 'O': cout << "\n\nPlayer O type in a move position: ";
break;
}
cin >> letterPosition >> numberPosition;
letterPosition = toupper(letterPosition); //convert to CAPS
//switch to position number
positionNumberOnBoard = switchCoordinatesToPosition(letterPosition, numberPosition, badMove);
//check for bad imput
if(badMove == -1)
{
cout << "That is an invalid move!";
badMove = -1;
}
}
while(badMove == -1);
}
//================================================================================
//function making the move
void makeTheMove(int positionNumberOnBoard, char playerValue)
{
switch(positionNumberOnBoard) {
case 1: p1 = playerValue; break;
case 2: p2 = playerValue; break;
case 3: p3 = playerValue; break;
case 4: p4 = playerValue; break;
case 5: p5 = playerValue; break;
case 6: p6 = playerValue; break;
case 7: p7 = playerValue; break;
case 8: p8 = playerValue; break;
case 9: p9 = playerValue; break;
default: cout << "Error !MaThMo!\n"; exit(-1); break;
}
}
//=================================================================================
int main()
{
p1=p2=p3=p4=p5=p6=p7=p8=p9=' '; //setting the globals to " "
bool notDone = true;
char playerValue = 'X'; //setting the player value to X, as X starts
//the game
char letterPosition; //value of the letter coordinate
int numberPosition; //value of the number coordinate
int positionNumberOnBoard;
int menuOption; //value for the beginning menu
int badMove;
displayInstructions(); //displaying instructions
displayMenu(menuOption); //displaying the menu
displayBoard();
while(notDone)
{
if(menuOption == 1)
{
displayMovePrompt(playerValue, letterPosition, numberPosition,
positionNumberOnBoard, badMove); //displaying which player's move
makeTheMove(positionNumberOnBoard, playerValue);
displayBoard();
//switchPlayers();
//checkForMill();
//areWeDone();
}
}
return 0;
}