My homework is to write a tic tac toe function, this code isn't working and I can not find the error. It prints the board and accepts user input just fine, the only problem is the checkBoard function. I can not get it to return a value(player win) to end the while loop.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int checkBoard(char board[3][3]) {
int i, j;
for (i = 0; i < 3; i += 1) {
if
((board[i][0]=='X'&&board[i][1]=='X'&&board[i][2]=='X'))
return 1;
if ((board[i][0] ==
'O'&&board[i][1] == 'O'&&board[i][2] == 'O'))
return 2;
}
for (j = 0; j < 3; j += 1) {
if
(board[0][j]=='X'&&board[1][j]=='X'&&board[1][j]=='X')
return 1;
if (board[0][j] ==
'O'&&board[1][j] == 'O'&&board[1][j] == 'O')
return 2;
}
if
(board[0][0]=='X'&&board[1][1]=='X'&&board[2][2]=='X')
return 1;
if (board[0][0] == 'O'&&board[1][1] ==
'O'&&board[2][2] == 'O')
return 2;
if (board[0][2] == 'X'&&board[1][1] ==
'X'&&board[2][0] == 'X')
return 1;
if (board[0][2] == 'X'&&board[1][1] ==
'X'&&board[2][0] == 'X')
return 2;
else return 0;
}
void printBoard(char board[3][3]) {
printf("%c | %c |
%c\n",board[0][0],board[1][0],board[2][0]);
printf("___|___|___\n");
printf("%c | %c | %c\n", board[0][1], board[1][1],
board[2][1]);
printf("___|___|___\n");
printf("%c | %c | %c\n", board[0][2], board[1][2],
board[2][2]);
printf(" | | \n\n\n\n");
}
int main() {
char board[3][3];
int i, j, x, y;
for (i = 0; i < 3; i += 1) {
for (j = 0; j < 3; j += 1)
{
board[j][i] = '
';
}
}
while (checkBoard(board) != 1 || checkBoard(board)
!= 2 || checkBoard(board) != 0) {
printBoard(board);
printf("X turn: Enter location 'x
y': ");
scanf("%d %d", &x,
&y);
while (board[x][y] == 'X' ||
board[x][y] == 'O') {
printf("Spot
taken choose again 'x y': ");
scanf("%d %d",
&x, &y);
}
board[x][y] = 'X';
printBoard(board);
checkBoard(board);
printf(" O turn: ");
scanf("%d %d", &x,
&y);
while (board[x][y] == 'X' ||
board[x][y] == 'O') {
printf("Spot
taken choose again 'x y': ");
scanf("%d %d",
&x, &y);
}
board[x][y] = 'O';
checkBoard(board);
}
printBoard(board);
if (checkBoard(board) == 1) {
printf("X's Win!\n");
}
if (checkBoard(board) == 2) {
printf("O's Win!\n");
}
if (checkBoard(board) == 0) {
printf("Tie game!\n");
}
return 0;
}
Program -
#include <stdio.h>
#include <conio.h>
char positionBoard[10] = { 'o', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
int checkBoard();
void printBoard();
int main()
{
int playerTurn = 1, i, userChoice;
char mark;
do
{
printBoard();
playerTurn = (playerTurn % 2) ? 1 : 2;
printf("Player %d, enter a number: ", playerTurn);
scanf("%d", &userChoice);
mark = (playerTurn == 1) ? 'X' : 'O';
if (userChoice == 1 && positionBoard[1] == '1')
positionBoard[1] = mark;
else if (userChoice == 2 && positionBoard[2] == '2')
positionBoard[2] = mark;
else if (userChoice == 3 && positionBoard[3] == '3')
positionBoard[3] = mark;
else if (userChoice == 4 && positionBoard[4] == '4')
positionBoard[4] = mark;
else if (userChoice == 5 && positionBoard[5] == '5')
positionBoard[5] = mark;
else if (userChoice == 6 && positionBoard[6] == '6')
positionBoard[6] = mark;
else if (userChoice == 7 && positionBoard[7] == '7')
positionBoard[7] = mark;
else if (userChoice == 8 && positionBoard[8] == '8')
positionBoard[8] = mark;
else if (userChoice == 9 && positionBoard[9] == '9')
positionBoard[9] = mark;
else
{
printf("Invalid move ");
playerTurn--;
getch();
}
i = checkBoard();
playerTurn++;
}while (i == - 1);
printBoard();
if (i == 1)
printf("==>\aPlayer %d win ", --playerTurn);
else
printf("==>\aGame draw");
getch();
return 0;
}
/*This function is used to check the game status and return 1 if
game is over -1 for game in progress 0 if game is over*/
int checkBoard()
{
if (positionBoard[1] == positionBoard[2] &&
positionBoard[2] == positionBoard[3])
return 1;
else if (positionBoard[4] == positionBoard[5] &&
positionBoard[5] == positionBoard[6])
return 1;
else if (positionBoard[7] == positionBoard[8] &&
positionBoard[8] == positionBoard[9])
return 1;
else if (positionBoard[1] == positionBoard[4] &&
positionBoard[4] == positionBoard[7])
return 1;
else if (positionBoard[2] == positionBoard[5] &&
positionBoard[5] == positionBoard[8])
return 1;
else if (positionBoard[3] == positionBoard[6] &&
positionBoard[6] == positionBoard[9])
return 1;
else if (positionBoard[1] == positionBoard[5] &&
positionBoard[5] == positionBoard[9])
return 1;
else if (positionBoard[3] == positionBoard[5] &&
positionBoard[5] == positionBoard[7])
return 1;
else if (positionBoard[1] != '1' && positionBoard[2] != '2'
&& positionBoard[3] != '3' &&
positionBoard[4] != '4' && positionBoard[5] != '5'
&& positionBoard[6] != '6' &&
positionBoard[7]
!= '7' && positionBoard[8] != '8' &&
positionBoard[9] != '9')
return 0;
else
return - 1;
}
//this function is used to draw board at with player mark on the
board
void printBoard()
{
system("cls");
printf("\n\n\tTic Tac Toe\n\n");
printf("Player 1 (X) - Player 2 (O)\n\n\n");
printf(" | | \n");
printf(" %c | %c | %c \n", positionBoard[1], positionBoard[2],
positionBoard[3]);
printf("_____|_____|_____\n");
printf(" | | \n");
printf(" %c | %c | %c \n", positionBoard[4], positionBoard[5], positionBoard[6]);
printf("_____|_____|_____\n");
printf(" | | \n");
printf(" %c | %c | %c \n", positionBoard[7], positionBoard[8], positionBoard[9]);
printf(" | | \n\n");
}
Screenshots -




My homework is to write a tic tac toe function, this code isn't working and I...
This is an advanced version of the game tic tac toe, where the player has to get five in a row to win. The board is a 30 x 20. Add three more functions that will check for a win vertically, diagonally, and backwards diagonally. Add an AI that will play against the player, with random moves. Bound check each move to make sure it is in the array. Use this starter code to complete the assignment in C++. Write...
JAVA TIC TAC TOE - please help me correct my code Write a program that will allow two players to play a game of TIC TAC TOE. When the program starts, it will display a tic tac toe board as below | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 The program will assign X to Player 1, and O to Player The program will ask Player 1, to...
In the C++ programming language write a program capable of playing 3D Tic-Tac-Toe against the user. Your program should use OOP concepts in its design. Use Inheritance to create a derived class from your Lab #9 Tic-Tac-Toe class. You can use ASCII art to generate and display the 3x3x3 playing board. The program should randomly decide who goes first computer or user. Your program should know and inform the user if an illegal move was made (cell already occupied). The...
Q1) Write a C function that allows the user to initialize a Tic-Tac-Toe board. The board is a 2D array of size 3x3. The function will set an id for each cell of the board starting from 1 and stop at 9. The function prototype is void InitializeBoard(int m, int n , char board[][n]) void InitializeBoard(int m, int n , char board[][n]){ int c =1; for(int i =0; i<m; i++){ for(int j=0; j< n; j++){ board[i][j] = c+'0';...
Can somebody help me with this coding the program allow 2 players play tic Tac Toe. however, mine does not take a turn. after player 1 input the tow and column the program eliminated. I want this program run until find a winner. also can somebody add function 1 player vs computer mode as well? Thanks! >>>>>>>>>>>>>Main program >>>>>>>>>>>>>>>>>>>>>>> #include "myheader.h" int main() { const int NUM_ROWS = 3; const int NUM_COLS = 3; // Variables bool again; bool won;...
In traditional Tic Tac Toe game, two players take turns to mark grids with noughts and crosses on the 3 x 3 game board. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row in the game board wins the game. Super Tic Tac Toe game also has a 3 x 3 game board with super grid. Each super grid itself is a traditional Tic Tac Toe board. Winning a game of Tic...
Hi, I am a student in college who is trying to make a tic tac toe game on Java. Basically, we have to make our own game, even ones that already exist, on Java. We also have to implement an interface, that I have no idea exactly. The professor also stated we need at least 2 different data structures and algorithms for the code. The professor said we could use code online, we'd just have to make some of our...
I just cannot figure out how to get the game to alternate turns. it is designed to be a 2 player game. the problem is once either an x or an o is placed it will onlycontinue to place the x or the o.please show me how to do the rest here is my code://#include <windows.h> // This header file will be needed for some windows compilers//#include <GL/gl.h> // gl.h and glu.h also maybe needed for some compilers//#include <GL/glu.h>#include <GL/glut.h>...
The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() { char board[SIZE]; int player, choice, win, count; char mark; count = 0; // number of boxes marked till now initBoard(board); // start the game player = 1; // default player mark = 'X'; // default mark do { displayBoard(board); cout << "Player " << player << "(" << mark...
You will create a PYTHON program for Tic-Tac-Toe game. Tic-Tac-Toe is normally played with two people. One player is X and the other player is O. Players take turns placing their X or O. If a player gets three of his/her marks on the board in a row, column, or diagonal, he/she wins. When the board fills up with neither player winning, the game ends in a draw 1) Player 1 VS Player 2: Two players are playing the game...