Reputation: 1
Im working on a project for my intro to C programming class and most of it I have been able to do, but just looking for some advice on how to reach the last couple steps since I've hit a wall. 1) It is a Tic Tac Toe program designed for two users to manually play, using their names. 2) This code must be modularized (at least for the game board and check winner functions) 3) Once a winner is declared, it should prompt the user if they would like to play again 4) If playing again, it must start with a new game board and keep track of games played and count the amount of times each player won.
So far my code is functional to take in and use players names, play the game itself, and check for a winner. The part I'm stuck at is figuring how to prompt to play again, and do that while counting and displaying the games played and the amount of times won for each player.
Any help and suggestions would be great! Thanks!
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// Prototypes
int check_winner(char game_score[9], char player1[10], char player2[10]);
char game_board(char game_score[9]);
// Begin main
int main(void)
{
// Initialize ad declare variables
char player1[10], player2[10];
char game_score[9] = {'1','2','3','4','5','6','7','8','9'};
int go, game_count, player1_wins, player2_wins;
int i, turn, play_again;
printf("\nWelcome to Tic-Tac-Toe!!\n\nWhat is Player 1's name? You will be X's: ");
fgets(player1, 10, stdin);
printf("\nWhat is Player 2's name? You will be O's: ");
fgets(player2, 10, stdin);
// Begin game that last 9 turns
for (turn = 1 ; turn <= 9 ; turn++)
{
// Display the initial game board
game_board(game_score);
// Prompt the first player to select where to go
printf("\n\n%s\nit is your turn! Please select the number where\n", player1);
printf("you want to place your X: ");
scanf("%d", &go);
// Place X in the selected spot on the board
game_score[go-1] = 'X';
// Check for a winner
check_winner(game_score, player1, player2);
// Display the game board updated with Player 1's move
game_board(game_score);
// Prompt Player 2 to select where to go
printf("\n\n%s\nit is your turn! Please select the number where\n", player2);
printf("you want to place your O: ");
scanf("%d", &go);
// Place O in selected spot on the board
game_score[go-1] = 'O';
// Check for a winner
check_winner(game_score, player1, player2);
}
return 0;
}
// Check winner function
int check_winner(char game_score[9], char player1[10], char player2[10])
{
int i;
if (game_score[0] == 'X' && game_score[3] == 'X' && game_score[6] == 'X')
printf("\n\n%syou are the winner! Would you like to play again?: ", player1);
else if (game_score[1] == 'X' && game_score[4] == 'X' && game_score[7] == 'X')
printf("\n\n%syou are the winner! Would you like to play again?: ", player1);
else if (game_score[2] == 'X' && game_score[5] == 'X' && game_score[8] == 'X')
printf("\n\n%syou are the winner! Would you like to play again?: ", player1);
else if (game_score[0] == 'X' && game_score[1] == 'X' && game_score[2] == 'X')
printf("\n\n%syou are the winner! Would you like to play again?: ", player1);
else if (game_score[3] == 'X' && game_score[4] == 'X' && game_score[5] == 'X')
printf("\n\n%syou are the winner! Would you like to play again?: ", player1);
else if (game_score[6] == 'X' && game_score[7] == 'X' && game_score[8] == 'X')
printf("\n\n%syou are the winner! Would you like to play again?: ", player1);
else if (game_score[0] == 'X' && game_score[4] == 'X' && game_score[8] == 'X')
printf("\n\n%syou are the winner! Would you like to play again?: ", player1);
else if (game_score[2] == 'X' && game_score[4] == 'X' && game_score[6] == 'X')
printf("\n\n%syou are the winner! Would you like to play again?: ", player1);
else if (game_score[0] == 'O' && game_score[3] == 'O' && game_score[6] == 'O')
printf("\n\n%syou are the winner! Would you like to play again?: ", player2);
else if (game_score[1] == 'O' && game_score[4] == 'O' && game_score[7] == 'O')
printf("\n\n%syou are the winner! Would you like to play again?: ", player2);
else if (game_score[2] == 'O' && game_score[5] == 'O' && game_score[8] == 'O')
printf("\n\n%syou are the winner! Would you like to play again?: ", player2);
else if (game_score[0] == 'O' && game_score[1] == 'O' && game_score[2] == 'O')
printf("\n\n%syou are the winner! Would you like to play again?: ", player2);
else if (game_score[3] == 'O' && game_score[4] == 'O' && game_score[5] == 'O')
printf("\n\n%syou are the winner! Would you like to play again?: ", player2);
else if (game_score[6] == 'O' && game_score[7] == 'O' && game_score[8] == 'O')
printf("\n\n%syou are the winner! Would you like to play again?: ", player2);
else if (game_score[0] == 'O' && game_score[4] == 'O' && game_score[8] == 'O')
printf("\n\n%syou are the winner! Would you like to play again?: ", player2);
else if (game_score[2] == 'O' && game_score[4] == 'O' && game_score[6] == 'O')
printf("\n\n%syou are the winner! Would you like to play again?: ", player2);
}
// Game board display function
char game_board(char game_score[9])
{
printf("\n\n %c | %c | %c \n---+---+---\n %c | %c | %c \n---+---+---\n %c | %c | %c \n",
game_score[0], game_score[1], game_score[2], game_score[3], game_score[4],
game_score[5], game_score[6], game_score[7], game_score[8]);
return 0;
}
Upvotes: 0
Views: 4053
Reputation: 330
Wrap your main()
function in a loop that exits when the user answers 'no' to the play again question. This will allow a player to keep answering 'yes' and continue playing.
Also make sure the main game loop (the for
loop in the main()
function) exits when a player wins. Otherwise games will continue after a player has won.
Upvotes: 0
Reputation: 3719
Put the contents of your main
function into a different function, which I'll call play_game
. In your new main
, run play_game
in a loop.
Upvotes: 1