简体   繁体   English

使用 C 的井字游戏

[英]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.问题的简要说明 - 代码的目的是使用 C 制作一个基本的井字游戏。有两个玩家XO ,每个人都可以输入从 1 到 9 的各种数字作为choice The game board is similar to a 3 x 3 matrix, where -游戏板类似于一个3 x 3矩阵,其中 -

  1. Row 1 is for 1 to 3.第 1 行是 1 到 3。
  2. Row 2 is for 4 to 6.第 2 行是 4 到 6。
  3. Row 3 is for 7 to 9.第 3 行是 7 到 9。

Any number except 1-9 will throw an error and will prompt the user to re-enter the number.除了 1-9 之外的任何数字都会抛出错误并提示用户重新输入数字。 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].您正在使用 square[1] 而不是 square[choice]。

Apparently 1 should work显然 1 应该工作

You also have good luck that you are not using array@0 since this would interfere with mark '0'.您也很幸运,您没有使用 array@0,因为这会干扰标记“0”。

Actually the whole if is useless you could just write something like实际上整个 if 没用,你可以写一些类似的东西

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

Ifs are code_pathes, code complexities is measuring in code path count如果是 code_pathes,则代码复杂性以代码路径计数来衡量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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