简体   繁体   中英

Tic Tac Toe using C

Brief Explanation of the Problem - The aim of the code is to make a basic tic tac toe game using C. There are two players X and O , both can enter various numbers as choice from 1-9 for each individual chance. The game board is similar to a 3 x 3 matrix, where -

  1. Row 1 is for 1 to 3.
  2. Row 2 is for 4 to 6.
  3. Row 3 is for 7 to 9.

Any number except 1-9 will throw an error and will prompt the user to re-enter the number. Unfortunately, I'm getting the same Invalid input error for a valid input. Everything else seems to work except my loop.

Here's the code for reference -

#include<stdio.h>     //Tic Tac Toe
#include<stdlib.h>
#include<conio.h>

char square[10] = {'0','1','2','3','4','5','6','7','8','9'};
int choice, player;

int checkForWin();
void displayBoard();
void mrkBoard(char mark);

int main()
{
 int i;
 char mark;
 player = 1;
 do
 {
  displayBoard();
  player = (player % 2) ? 1:2;
  printf("Player %d, enter the number: ",player);
  scanf("%d",&choice);
  mark = (player == 1) ? 'X' : 'O';
  mrkBoard(mark);
  i = checkForWin();
  player++;
 }while(i == -1);
 return 0;
}

int checkForWin()
{
  int returnValue = 0;
  if (square[1] == square[2] && square[2] == square[3])
  {
    returnValue = 1;
  }
  else if (square[4] == square[5] && square[5] == square[6])
   returnValue = 1;
  else if (square[7] == square[8] && square[8] == square[9])
   returnValue = 1;
  else if (square[1] == square[5] && square[5] == square[9])
  returnValue = 1;
  else if (square[3] == square[5] && square[5] == square[7])
  returnValue = 1;
  else if (square[1] == square[4] && square[4] == square[7])
  returnValue = 1;
  else if (square[2] == square[5] && square[5] == square[8])
  returnValue = 1;
  else if (square[3] == square[6] && square[6] == square[9])
  returnValue = 1;
  else if(square[1] != '1' && square[2] != '2' && square[3] != '3' && square[4] != '4' &&
  square[5] != '5' && square[6] != '6' && square[7] != '7' &&
  square[8] != '8' && square[9] != '9')
  returnValue = 0;
  else
  returnValue = -1;
  return returnValue;
}

void displayBoard()
{
  system("cls");
  printf("\n\nTic Tac Toe\n\n");

  printf("Player 1 (X) - Player 2 (O)\n\n\n");

  printf("     |     |     \n");
  printf("  %c  |  %c  |  %c \n", square[1], square[2],square[3]);

  printf("_____|_____|_____\n");
  printf("     |     |     \n");

  printf("  %c  |  %c  |  %c\n", square[4], square[5],square[6]);

  printf("_____|_____|_____\n");
  printf("     |     |     \n");

  printf("  %c  |  %c  |  %c\n", square[7], square[8],square[9]);
  printf("     |     |     \n\n");
}

void mrkBoard(char mark)
{
 if (choice == 1 && square[1] == '1')
 square[1] = mark;
 else if (choice == 2 && square[1] == '2')
 square[2] = mark;
 else if (choice == 3 && square[1] == '3')
 square[3] = mark;
 else if (choice == 4 && square[1] == '4')
 square[4] = mark;
 else if (choice == 5 && square[1] == '5')
 square[5] = mark;
 else if (choice == 6 && square[1] == '6')
 square[6] = mark;
 else if (choice == 7 && square[1] == '7')
 square[7] = mark;
 else if (choice == 8 && square[1] == '8')
 square[8] = mark;
 else if (choice == 9 && square[1] == '9')
 square[9] = mark;

 else
 {
   printf("Invalid ");
   player--;
   getch();
 }
}
#include<stdio.h>     //Tic Tac Toe
#include<stdlib.h>
#include<conio.h>

char square[10] = {'0','1','2','3','4','5','6','7','8','9'};
int choice, player;
int checkForWin();
void displayBoard();
void mrkBoard(char mark);

int main()
{
 int i;
 char mark;
 player = 1;
 do
 {
  displayBoard();
  player = (player % 2) ? 1:2;
  printf("Player %d, enter the number: ",player);
  scanf("%d",&choice);
  mark = (player == 1) ? 'X' : 'O';
  mrkBoard(mark);
  i = checkForWin();
  player++;
 }while(i == -1);
 return 0;
}

int checkForWin()
{
  int returnValue = 0;
  if (square[1] == square[2] && square[2] == square[3])
  {
    returnValue = 1;
  }
  else if (square[4] == square[5] && square[5] == square[6])
   returnValue = 1;
  else if (square[7] == square[8] && square[8] == square[9])
   returnValue = 1;
  else if (square[1] == square[5] && square[5] == square[9])
  returnValue = 1;
  else if (square[3] == square[5] && square[5] == square[7])
  returnValue = 1;
  else if (square[1] == square[4] && square[4] == square[7])
  returnValue = 1;
  else if (square[2] == square[5] && square[5] == square[8])
  returnValue = 1;
  else if (square[3] == square[6] && square[6] == square[9])
  returnValue = 1;
  else if(square[1] != '1' && square[2] != '2' && square[3] != '3' && square[4] != '4' &&
  square[5] != '5' && square[6] != '6' && square[7] != '7' &&
  square[8] != '8' && square[9] != '9')
  returnValue = 0;
  else
  returnValue = -1;
  return returnValue;
}

void displayBoard()
{
  system("cls");
  printf("\n\nTic Tac Toe\n\n");

  printf("Player 1 (X) - Player 2 (O)\n\n\n");

  printf("     |     |     \n");
  printf("  %c  |  %c  |  %c \n", square[1], square[2],square[3]);

  printf("_____|_____|_____\n");
  printf("     |     |     \n");

  printf("  %c  |  %c  |  %c\n", square[4], square[5],square[6]);

  printf("_____|_____|_____\n");
  printf("     |     |     \n");

  printf("  %c  |  %c  |  %c\n", square[7], square[8],square[9]);
  printf("     |     |     \n\n");
}

void mrkBoard(char mark)
{
 if (choice == 1 && square[1] == '1')
 square[1] = mark;
 else if (choice == 2 && square[2] == '2')
 square[2] = mark;
 else if (choice == 3 && square[3] == '3')
 square[3] = mark;
 else if (choice == 4 && square[4] == '4')
 square[4] = mark;
 else if (choice == 5 && square[5] == '5')
 square[5] = mark;
 else if (choice == 6 && square[6] == '6')
 square[6] = mark;
 else if (choice == 7 && square[7] == '7')
 square[7] = mark;
 else if (choice == 8 && square[8] == '8')
 square[8] = mark;
 else if (choice == 9 && square[9] == '9')
 square[9] = mark;

 else
 {
   printf("Invalid ");
   player--;
   getch();
 }
}

You are trying to check if it has been already marked.

You are using square[1] instead of square[choice].

Apparently 1 should work

You also have good luck that you are not using array@0 since this would interfere with mark '0'.

Actually the whole if is useless you could just write something like

if (square[choice]==('0'+choice)){
square[choice]=mark
} else {
//invalid
}

Ifs are code_pathes, code complexities is measuring in code path count

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM