Almost done with the Tic Tac Toe, but I need 1 more easy thing. PLEASE HELP!!!
Ok here is my entire Program below. BUT I cant figure out how to get the damn thing to quit! Its having trouble finding the variable to end it. I can get it to end and find a winner but it wont quit properly. Ive spent about 16 hours at least in the past 2 days with my cousins help.
I plead with everyone here to please look at this program, load it up if needed and help me figure out this seemingly small problem. It defenately needs some C Programming exprets here :)
Thank you very much and my entire program is below so anyone and everyone can critique it and help. Thanks to anyone who helps. Its greatly appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void Initialize_Board(char [3][3]);
void get_Player1_move(char [3][3]);
void get_Player2_move(char [3][3]);
void disp_board(char [3][3]);
char check(char [3][3], int answer);
int Ask_for_new_game(char [3][3], int answer);
void main (int answer)
{
int newgame;
char ttt[3][3]; /* Tic Tac Toe Board */
char winner;
printf("Welcome to Tic-Tac-Toe Game!\n\n");
printf("Here is the board numbering (Row Column):\n");
printf(" 0 1 2 \n");
printf(" *************\n");
printf("0 *0 0|0 1|0 2*\n");
printf(" *---+---+---*\n");
printf("1 *1 0|1 1|1 2*\n");
printf(" *---+---+---*\n");
printf("2 *2 0|2 1|2 2*\n");
printf(" *************\n");
printf("\nPlayer 1 will be 'X' Player 2 will be 'O'.\n");
printf("Would you like to play a game? (Type a 1 for Yes and a 2 for No): ");
scanf("%d", &newgame);
if (newgame != 2)
{
printf("***************************\n");
printf("** Let's start the Game. **\n");
printf("***************************\n");
Initialize_Board(ttt); /* Initialize the Game Board */
/* Loop until Game Over */
do
{
disp_board(ttt); /* display the Game Board */
get_Player1_move(ttt); /* ask Player1 for a move */
check(ttt, answer); /* check for winner */
if(answer == 2) break;
disp_board(ttt);
get_Player2_move(ttt); /* ask Player2 for a move */
check(ttt, answer); /* check for winner */
} while(answer != 2);
}
else
printf("\n\n **************************************\n");
printf(" ** Thank you for Playing. GoodBye! **\n");
printf(" **************************************\n");
exit;
}
/* initialize the matrix to blanks */
void Initialize_Board(char ttt[3][3])
{
int x, y;
for (x = 0; x < 4; x++)
{
for (y = 0; y < 4; y++)
ttt[x][y] = ' ';
}
}
/* Get the move for player1 */
void get_Player1_move(char ttt[3][3])
{
int x, y;
/* for(;;)
{ */
printf("Player1 please enter your move coordinates using the format (row column): ");
scanf("%d%d", &x, &y);
/* if ((x < 0) || (x > 3) || (y < 0) || (y > 3))
{
break;
printf("\nYou have entered Illegal coordinates, Please try again\n");
}
}
x--;
y--;
if(board[x][y] != ' ')
{
printf("Invalid move, Please try again.\n");
get_Player1_move(board);
}
else */
ttt[x][y] = 'X';
}
/* Get the move for player2 */
void get_Player2_move(char ttt[3][3])
{
int x, y;
/* for(;;)
{ */
printf("Player2 please enter your move coordinates using the format (row column): ");
scanf("%d%d", &x, &y);
/* if ((x < 0) || (x > 3) || (y < 0) || (y > 3)) break;
printf("\nYou have entered Illegal coordinates, Please try again\n");
}
x--;
y--;
if(board[x][y] != ' ')
{
printf("Invalid move, Please try again.\n");
get_Player2_move(board);
}
else */
ttt[x][y] = 'O';
}
/* Display the board on the screen */
void disp_board(char ttt[3][3])
{
printf(" 0 1 2 \n");
printf(" *************\n");
printf("0 * %c | %c | %c *\n", ttt[0][0], ttt[0][1], ttt[0][2]);
printf(" *---+---+---*\n");
printf("1 * %c | %c | %c *\n", ttt[1][0], ttt[1][1], ttt[1][2]);
printf(" *---+---+---*\n");
printf("2 * %c | %c | %c *\n", ttt[2][0], ttt[2][1], ttt[2][2]);
printf(" *************\n");
}
/* See if there is a winner. */
char check(char ttt[3][3], int answer)
{
char winner;
/* Check for a wining line - Diagonals first */
if((ttt[0][0] == 'X') && (ttt[1][1] == 'X') && (ttt[2][2] == 'X') ||
(ttt[0][2] == 'X') && (ttt[1][1] == 'X') && (ttt[2][0] == 'X'))
{
printf("\n\nThe winner is Player 1!\n\n");
Ask_for_new_game(ttt, answer);
}
if ((ttt[0][0] == 'O') && (ttt[1][1] == 'O') && (ttt[2][2] == 'O') ||
(ttt[0][2] == 'O') && (ttt[1][1] == 'O') && (ttt[2][0] == 'O'))
{
printf("\n\nThe winner is Player 2!\n\n");
Ask_for_new_game(ttt, answer);
}
/* Check for a wining line - Horizontal Rows */
if((ttt[0][0] == 'X') && (ttt[0][1] == 'X') && (ttt[0][2] == 'X') ||
(ttt[1][0] == 'X') && (ttt[1][1] == 'X') && (ttt[1][2] == 'X') ||
(ttt[2][0] == 'X') && (ttt[2][1] == 'X') && (ttt[2][2] == 'X'))
{
printf("\n\nThe winner is Player 1!\n\n");
Ask_for_new_game(ttt, answer);
}
if ((ttt[0][0] == 'O') && (ttt[0][1] == 'O') && (ttt[0][2] == 'O') ||
(ttt[1][0] == 'O') && (ttt[1][1] == 'O') && (ttt[1][2] == 'O') ||
(ttt[2][0] == 'O') && (ttt[2][1] == 'O') && (ttt[2][2] == 'O'))
{
printf("\n\nThe winner is Player 2!\n\n");
Ask_for_new_game(ttt, answer);
}
/* Check for a wining line - Vertical Rows */
if((ttt[0][0] == 'X') && (ttt[1][0] == 'X') && (ttt[2][0] == 'X') ||
(ttt[0][1] == 'X') && (ttt[1][1] == 'X') && (ttt[2][1] == 'X') ||
(ttt[0][2] == 'X') && (ttt[1][2] == 'X') && (ttt[2][2] == 'X'))
{
printf("\n\nThe winner is Player 1!\n\n");
Ask_for_new_game(ttt, answer);
}
if ((ttt[0][0] == 'O') && (ttt[1][0] == 'O') && (ttt[2][0] == 'O') ||
(ttt[0][1] == 'O') && (ttt[1][1] == 'O') && (ttt[2][1] == 'O') ||
(ttt[0][2] == 'O') && (ttt[1][2] == 'O') && (ttt[2][2] == 'O'))
{
printf("\n\nThe winner is Player 2!\n\n");
Ask_for_new_game(ttt, answer);
}
}
int Ask_for_new_game(char ttt[3][3], int answer)
{
printf("\n\nWould you like to play another game? (Enter a 1 for Yes and a 2 for No): \n");
scanf("%d", &answer);
if (answer == 1)
{
Initialize_Board(ttt);
}
if (answer == 2)
{
printf("\n\n\nthank you for playing Tic Tac Toe, Goodbye!\n\n\n");
return answer;
}
}
I can't post a new thread for some reason...
I can't post a new thread for some reason(page wont load). So I hope this message makes it somewhere someone can read it.
I am trying to multiply two 3x3 arrays. All that I know how to do is add array's, so I am a little lost. Any help would be great. Also if anyone wants, if they can, please copy & post this message as a new thread, that would be great.