gpt4 book ai didi

c - 用c语言简单连接四个游戏

转载 作者:行者123 更新时间:2023-11-30 16:09:02 25 4
gpt4 key购买 nike

在这个游戏中,您有一个 9 列、6 行的棋盘。一开始,第一列和最后一列交替填充玩家标记。计算机将随机选择哪个玩家(人类或计算机)先移动。该玩家将其代币放入棋盘中间 7 列之一。 (最左边和最右边的列已经满了)。然后下一个玩家做同样的事情。在任何时候,如果玩家丢下一个 token ,连续完成一组 5 个 token (水平、垂直或对角线),则该玩家赢得游戏。

我无法使用除 printf()scanf()putc()getc()< 以外的库函数。我在对角线检查方面遇到一些问题,请帮助我这是我当前的代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BOARD_ROWS 6
#define BOARD_COLS 9

void printBoard(char *board);
int takeTurn(char *board, int player, const char*);
int checkWin(char *board);
int checkfour(char* board , int,int,int,int);
int horizontalCheck(char *board);
int verticalCheck(char *board);
int diagonalCheck (char *board);

int main(int argc, char *argv[]){
const char *PIECES = "XO";
char board[BOARD_ROWS*BOARD_COLS];
for(int i =0; i< BOARD_ROWS*BOARD_COLS ; i ++){
board[i] = '.';
}
int turn, done=0;

for (turn=0; turn < BOARD_ROWS*BOARD_COLS && !done; turn++){
printBoard(board);
while(!takeTurn(board,turn %2, PIECES))
{
printBoard(board);
puts("**Column full!**\n");
}
done = checkWin(board);
}
printBoard(board);
if (turn == BOARD_ROWS*BOARD_COLS && !done)
{
puts("It's a tie!");
}

else{
turn--;
printf("player %d (%c) wins!\n", turn %2 + 1, PIECES[turn%2]);
}
return 0;
}

void printBoard(char *board){
int row,col;
puts("\n *****Connect tour*****\n");
for(row=0; row < BOARD_ROWS; row++){
for(col =0; col <BOARD_COLS;col++){
printf(" %c",board[BOARD_COLS * row+col]);
}
puts(" ");
//puts(".......");
}
puts(" ");
puts(" 1 2 3 4 5 6 7 8 9 \n");
}

int takeTurn(char*board, int player, const char *PIECES)
{
int row,col = 0;
printf("Player %d (%c): \nEnter number Coordinate:", player+1, PIECES[player]);
while(1){
if (1 != scanf("%d", &col) || col < 1 || col >9){
while(getchar() != '\n');
puts("number out of bounds! Try again.");
}
else{
break;
}
}
col--;
for(row = BOARD_ROWS-1; row>= 0; row--){
printf("here");
if(board[BOARD_COLS * row+col] == '.'){
board[BOARD_COLS * row+col] = PIECES[player];
return 1;
}
}
return 0;
}
int checkWin(char *board){
return(horizontalCheck(board)|| verticalCheck(board) || diagonalCheck(board));
}

int checkfour(char *board, int a, int b, int c , int d){
if (board[a] == board[b] && board[b]==board[c] && board[c]==board[d]&&board[a] != '.'){
return 1;
}
return 0;
}
int horizontalCheck(char *board){
int row,col,idx;
const int WIDTH = 1;
for (row=0; row<BOARD_ROWS; row++){
for(col = 0; col < BOARD_COLS-3; col++){
idx = BOARD_COLS * row+col;
if(checkfour(board, idx, idx + WIDTH, idx + WIDTH *2, idx + WIDTH*3)){
return 1;
}
}
}
return 0;
}

int verticalCheck(char*board){
int row,col,idx;
const int HEIGHT = 7;
for(row = 0; row< BOARD_ROWS-3; row++){
for(col = 0; col < BOARD_COLS;col++){
idx = BOARD_COLS*row+col;
if (checkfour(board, idx,idx+HEIGHT, idx+ HEIGHT*2, idx+ HEIGHT*3)){
return 1;
}
}
}
return 0;
}

int diagonalCheck(char *board){
int row,col,idx,Count = 0;
const int DIAG_RGT = 6, DIAG_LFT = 8;
for (row = 0; row < BOARD_ROWS-3; row++){
for(col = 0; col<BOARD_COLS;col++){
idx = BOARD_COLS*row+col;
if ((Count <=3 && checkfour(board, idx, idx+DIAG_LFT , idx+DIAG_LFT*2, idx + DIAG_LFT*3))||(
Count >=3&&checkfour(board,idx,idx+DIAG_RGT,idx+DIAG_RGT*2, idx+DIAG_RGT*3))){
return 1;
}
Count++;
}
Count = 0;
}
return 0;
}

最佳答案

你的内心for for(col = 0; col<BOARD_COLS;col++){直到 BOARD_COLS而不是BOARD_COLS-3 。在内if您需要检查是否存在这样的对角线进入一个方向( DIAG_RGT )或另一个方向( DIAG_LFT )。这是有道理的,在第一个 if你会使用DIAG_RGT计算索引并在其他 if 中你会使用DIAG_LFT .

关于c - 用c语言简单连接四个游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59225020/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com