Tic Tac Toe Assignment

Im in a c++ class in college and need some help with this code. I did the assignment based off what i have learned in the class so far and obviously need to do more research in the book. Here is the assignment and what i have so far..

Program must use functions and CANNOT be written as one main program.
Use a function to display a set of instructions describing how the user must enter a "move" (this has to called only once at the beginning of the program)
Use a function to display the contents of the board. (Each element of the array should be initialized with an asterisk(*).
The program must clear the display screen after the user enters a new move, then display the contents of the board with the new move shown.
Use a function to describe the actions taken in each player's turn.
Validate each users move (i.e. if the user enters a location that already contains an X or an O or the numbers entered are not a valid position on the board an error will be displayed). The program must then ask the user to re-input their move.
Use a function to determine whether the player has won. The function should receive as arguments, the board and the player ( 'X' or 'O'). You also check for a Tie or End of Game in this function or create a separate function to do this.
The program should ask the player to enter the row and column number.
The board and the elements contained within the board must line up correctly.
The program MUST use a 2D Array to represent the playing board. Remember that C++ arrays are indexed starting with a subscript of 0 (zero). However the user is not expected to know that, so the rows and columns should be referred to in prompts as 1-3 rather than 0-2. See the illustrations below.
Determine whether a player has won or there is a tie. If there is a winner, the program should display that a player has won the game and which player has won. If there is a tie the program should display that a tie has occured an exit.



#include <iostream>
#include <conio.h>
#include <cstdlib>

using namespace std;
char square[10] = {'o','1','2','3','4','5','6','7','8','9'};
int checkwin();
void board();
int row;
int col;
int main()

{
int player = 1,O,i;

char mark;
system("CLS");
do
{
(player 1 == X);
(player 2 == O);
board();
player=(player%2)1:2;
cout << "Player " << player << "'s turn. \nEnter a row and column.\n";
cout << "Row: ";
cin >> row;
cout << "Column: ";
cin >> col;
mark=(player == 1) ? 'X' : 'O';
if (row == 1 && col == 1 && square[1] == '1')
square[1] = mark;
else if (row == 1 && col == 2 && square[2] == '2')
square[2] = mark;
else if (row == 1 && col == 3 && square[3] == '3')
square[3] = mark;
else if (row == 2 && col == 1 && square[4] == '4')
square[4] = mark;
else if (row == 2 && col ==2 && square[5] == '5')
square[5] = mark;
else if (row == 2 && col == 3 && square[6] == '6')
square[6] = mark;
else if (row == 3 && col ==1 && square[7] == '7')
square[7] = mark;
else if (row == 3 && col == 2 && square[8] == '8')
square[8] = mark;
else if (row == 3 && col == 3 && square[9] == '9')
square[9] = mark;
else
{
cout<<"That location is not available. Please select another location. ";
player--;
getch();
}
i=checkwin();
player++;
}while(i==-1);
board();
if(i==1)
cout<<"==>\aPlayer "<<--player<<" win ";
else
cout<<"==>\aGame draw";
getch();
return 0;
}


int checkwin()
{
if (square[1] == square[2] && square[2] == square[3])
return 1;
else if (square[4] == square[5] && square[5] == square[6])
return 1;
else if (square[7] == square[8] && square[8] == square[9])
return 1;
else if (square[1] == square[4] && square[4] == square[7])
return 1;
else if (square[2] == square[5] && square[5] == square[8])
return 1;
else if (square[3] == square[6] && square[6] == square[9])
return 1;
else if (square[1] == square[5] && square[5] == square[9])
return 1;
else if (square[3] == square[5] && square[5] == square[7])
return 1;
else if (square[1] != '1' && square[2] != '2' && square[3] != '3' && square[4] != '4'
&& square[5] != '5' && square[6] != '6' && square[7] != '7' && square[8] != '8' && square[9] != '9')
return 0;
else
return -1;
}


void board()
{
system("CLS");
cout << "\n\n\tTic Tac Toe\n\n";
cout << "Player 1 (X) - Player 2 (O)" << endl << endl;
cout << " Columns\n";
cout << " 1 2 3\n";
cout << "Row 1";
cout << " " << square[1] << " " << square[2] << " " << square[3] << endl;
cout << "Row 2";
cout << " " << square[4] << " " << square[5] << " " << square[6] << endl;
cout << "Row 3";
cout << " " << square[7] << " " << square[8] << " " << square[9] << endl;

}
Topic archived. No new replies allowed.