How would I go about writing code to check if there is a winnerbetween two playe
ID: 3609035 • Letter: H
Question
How would I go about writing code to check if there is a winnerbetween two players on a tick tac toe board with arrays?#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define XPLAYER 0
#define OPLAYER 1
#define OPEN -99
#define TIEGAME 2
#define LIVEGAME 3
int gameboard[9];
// Prototypes of subroutines involved
void initialize_board();
int check_winner();
int check_valid_move();
void display_board();
// Tic-Tac-Toe
//
// | |
// ------------
// | |
// ------------
// | |
//
// The array "gameboard" is tracking the following:
// 0 | 1 | 2
// ------------
// 3 | 4 | 5
// ------------
// 6 | 7 | 8
//
// The array "gameboard" is tracking the following:
int check_winner()
{
//write code to see if player has wongame
// Return 0 if game is won by XPLAYER
// Return 1 if game is won by OPLAYER
// Return 2 if game is a tie
// Return 3 if game is still going on
}
Explanation / Answer
So far I've come up with... int check_winner() { // STUDENT : write code to see if player has won game // Return 0 if game is won by XPLAYER // Return 1 if game is won by OPLAYER // Return 2 if game is a tie // Return 3 if game is still going on { // To check if XPLAYER has won if (gameboard[0][0] == 'X' && gameboard[0][1] == 'X'&& gameboard[0][2] == 'X' || gameboard[1][0] == 'X' &&gameboard[1][1] == 'X' && gameboard[1][2] == 'X' || gameboard[2][0] == 'X' &&gameboard[2][1] == 'X' && gameboard[2][2] == 'X' || gameboard[0][0] == 'X' &&gameboard[1][0] == 'X' && gameboard[2][0] == 'X' || gameboard[0][1] == 'X' &&gameboard[1][1] == 'X' && gameboard[2][1] == 'X' || gameboard[0][2] == 'X' &&gameboard[1][2] == 'X' && gameboard[2][2] == 'X' || gameboard[0][0] == 'X' &&gameboard[1][1] == 'X' && gameboard[2][2] == 'X' || gameboard[0][2] == 'X' &&gameboard[1][1] == 'X' && gameboard[2][0] == 'X') return 0; // To check if OPLAYER has won else if (gameboard[0][0] == 'O' && gameboard[0][1] == 'O'&& gameboard[0][2] == 'O' || gameboard[1][0] =='O' && gameboard[1][1] == 'O' && gameboard[1][2] =='O' || gameboard[2][0] =='O' && gameboard[2][1] == 'O' && gameboard[2][2] =='O' || gameboard[0][0] =='O' && gameboard[1][0] == 'O' && gameboard[2][0] =='O' || gameboard[0][1] =='O' && gameboard[1][1] == 'O' && gameboard[2][1] =='O' || gameboard[0][2] =='O' && gameboard[1][2] == 'O' && gameboard[2][2] =='O' || gameboard[0][0] =='O' && gameboard[1][1] == 'O' && gameboard[2][2] =='O' || gameboard[0][2] =='O' && gameboard[1][1] == 'O' && gameboard[2][0] =='O') return 1; // To check for a tie else if (gameboard[0][0] != ' ' && gameboard[0][1] != ' '&& gameboard[0][2] != ' ' && gameboard[1][0] !=' ' && gameboard[1][1] != ' ' && gameboard[1][2] !=' ' && gameboard[2][0] !=' ' && gameboard[2][1] != ' ' && gameboard[2][2] !=' ') return 2; // Otherwise continue.. else return 3; } I'm a very beginner and any advice would be helpful
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.