Thanks 0x90! That really helped! So the program compiles, but I am getting a bunch of garbage when I run the program....I'm really stuck here.
Here is some of the results I'm getting when I run it...
Code:
grid3.cs: a.out
Welcome to Tic-Tac-Toe
Three in a row wins!
1). Play a game
2). Quit
Please make a selection: 1
| |
---------
| |
---------
| |
Enter location (1-9) = 9
Enter location (1-9) = 4
x Wins!
xCat's game!
Welcome to Tic-Tac-Toe
Three in a row wins!
1). Play a game
2). Quit
Please make a selection: 5
| |
---------
| |
---------
| |
Enter location (1-9) = clear
Enter location (1-9) = x Wins!
xCat's game!
Welcome to Tic-Tac-Toe
Three in a row wins!
1). Play a game
2). Quit
Please make a selection: | |
---------
| |
---------
| |
Segmentation Fault (core dumped)
Heh, something is obviously wrong in my code...I just don't know what it is. If you guys have any suggestions, please let me know!!
Thanks!
Here's my new code:
Code:
//Tic Tac Toe
//Written by Jerad
//Function prototypes.
void drawBoard(char board[]);
int getMove(char board[], char turn);
bool checkWin(char board[], char turn);
bool playGame(void);
#include<iostream>
using namespace std;
//Main function.
int main()
{
char turn;
int numTurns;
int move;
int x_wins;
int o_wins;
int no_wins;
//Main loop, calls the menu function.
while(playGame())
{
//Initialze board.
char board[] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
bool win = false;
turn = 'x';
for (numTurns = 1; numTurns < 10; numTurns++)
{
drawBoard(board); //Display board.
move = getMove(board, turn);
board[move] = turn;
win = checkWin(board, turn); //Check for win.
if (win) break;
}
if (turn == 'x'){
x_wins++;
cout << turn << " Wins!" << endl;
}
else if (turn == 'o'){
o_wins++;
cout << turn << " Wins!" << endl;
}
else
no_wins = 0;
cout << turn << "Cat's game!" << endl;
}
cout << "X wins: " << x_wins << endl;
cout << "O wins: " << o_wins << endl;
cout << "Non-wins: " << no_wins << endl;
}
//Menu Function.
bool playGame(void)
{
int selection;
cout << endl;
cout << "Welcome to Tic-Tac-Toe" << endl;
cout << "Three in a row wins!" << endl;
cout << endl;
cout << "1). Play a game" << endl;
cout << "2). Quit" << endl;
cout << endl;
cout << "Please make a selection: ";
cin >> selection;
if (selection == 1);
return true;
}
//Display Board Function.
void drawBoard(char board[])
{
cout << board[1] << " | " << board[2] << " | " << board[3] << endl;
cout << "---------" << endl;
cout << board[4] << " | " << board[5] << " | " << board[6] << endl;
cout << "---------" << endl;
cout << board[7] << " | " << board[8] << " | " << board[9] << endl;
}
//Get position from player.
int getMove(char board[], char turn)
{
int move;
cout << "Enter location (1-9) = ";
cin >> move;
do{
cout << "Enter location (1-9) = ";
cin >> move;
}
while ((move < 1) || (move > 9) && (board[move] != ' '));
do{
return move;
}
while ((move > 1) || (move < 9) && (board[move] == ' '));
}
//Check for a win.
bool checkWin(char board[], char turn)
{
if (((board[1] == board[2]) && (board[2] == board[3])) && (board[3] == turn));
else if (((board[4] == board[5]) && (board[5] == board[6])) && (board[6] == turn));
else if (((board[7] == board[8]) && (board[8] == board[9])) && (board[9] == turn));
else if (((board[1] == board[5]) && (board[5] == board[9])) && (board[9] == turn));
else if (((board[3] == board[5]) && (board[5] == board[7])) && (board[7] == turn));
return true;
}