任务
用C语言设计一个井字棋游戏,包含以下内容:
1.用结构体设计一个可以创建账户名称,密码累积输赢数字且账户相关信息能够存储在数据文件中,并由程序访问
2.设计一个3*3井字棋游戏,包括游戏开始菜单设计,棋盘的生成与初始化与棋盘打印,玩家下棋和电脑下棋,判断输赢和统计输赢次数,且在开始游戏后可以选择玩家先手或电脑先手
本人写了一段C语言代码,但是代码无法运行,求各位帮我看看我的程序的问题在哪以及如何改正,以及创建账户和密码和统计输赢次数的部分如何实现?
本人代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
typedef struct The_users
{
char id[15];
char pwd[20];
}users;
void Create_File()
{
FILE *fp;
if ((fp = fopen("users.txt","rb"))==NULL)
{
if ((fp = fopen("users.txt","wb+"))==NULL)
{
printf("Unable to create a file!\n");
exit(0);
}
}
}
void registers()
{
users c,d;
FILE *fp;
char temp[20];
int count = 0;
printf("Welcome to the registration screen!\n");
Sleep(1000);
fp = fopen("users.txt","r");
fread(&d, sizeof(struct The_users), 1, fp);
printf("Please enter your account number\n");
scanf("%s",&c.id);
while (1)
{
if (strcmp(c.id, d.id))
{
if (!feof(fp))
{
fread(&d, sizeof(struct The_users), 1, fp);
}
else
break;
}
else
{
printf("This username already exists!\n");
Sleep(1000);
fclose(fp);
return;
}
}
printf("Please enter your password\n");
scanf("%s",&c.pwd);
printf("Please confirm the password\n");
scanf("%s",&temp);
do{
if(!strcmp(c.pwd,temp)){
fp = fopen("users.txt","c");
fwrite(&c, sizeof(struct The_users), 1, fp);
printf("Account registration successful!\n");
Sleep(500);
fclose(fp);
return;
}else{
printf("Password does not match, please retype£¡\n");
scanf("%s",&c.pwd);
printf("Please confirm the password\n");
scanf("%s",&temp);
}
}while(1);
}
void Input_login()
{
users c,d;
FILE *fp;
printf("Welcome to the login screen!\n");
Sleep(1000);
fp = fopen("users.txt","r");
fread(&d, sizeof(struct The_users), 1, fp);
printf("Please enter your account number\n");
scanf("%s",&c.id);
while (1)
{
if (strcmp(c.id, d.id)==0)
{
break;
}
else
{
if (!feof(fp))
{
fread(&d, sizeof(struct The_users), 1, fp);
}
else
{
printf("User name does not exist, please re-enter£¡\n");
Sleep(500);
fclose(fp);
return;
}
}
}
printf("Please enter your password\n");
scanf("%s",&c.pwd);
do{
if (strcmp(c.pwd, d.pwd)==0)
{
fclose(fp);
printf("Login successfully!");
Sleep(500);
return;
}
else
{ printf("The password is incorrect, please re-enter the password\n");
scanf("%s",&c.pwd);
}
}while(strcmp(c.pwd, d.pwd)==0);
}
void menu()
{
printf("****************************\n");
printf("** 1.Start the game **\n");
printf("****************************\n");
printf("** 0.Exit the game **\n");
printf("****************************\n");
}
int whom_act()
{
int input = 0;
printf("****************************\n");
printf("** 1.Computer first chess **\n");
printf("****************************\n");
printf("** 0.Players play chess first **\n");
printf("****************************\n");
while (1)
{
printf("Please choose who plays first:>");
scanf_s("%d", &input);
if (input == 1)
return 1;
else if (input == 0)
return 0;
else
printf("An error occurred, please reselect\n");
}
}
void init_chess(char arr[3][3])
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
arr[i][j] = ' ';
}
}
}
void print_chess(char arr[3][3])
{
printf("\n");
for (int i = 0; i < 3; i++)
{
printf("%c | %c| %c\n", arr[i][0], arr[i][1], arr[i][2]);
if (i < 2)
{
printf("—+—+—\n");
}
}
printf("\n");
}
int chess_full(char arr[3][3])
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (arr[i][j] == ' ')
return 0;
}
}
return 1;
}
char judge_win(char arr[3][3])
{
for (int i = 0; i < 3; i++)
{
if ((arr[i][0] == arr[i][1]) && (arr[i][1] == arr[i][2]) && (arr[i][0] != ' '))
return arr[i][0];
else if ((arr[0][i] == arr[1][i]) && (arr[1][i] == arr[2][i]) && (arr[0][i] != ' '))
return arr[0][i];
else if ((arr[0][0] == arr[1][1]) && (arr[1][1] == arr[2][2]) && (arr[1][1] != ' '))
return arr[1][1];
else if ((arr[0][2] == arr[1][1]) && (arr[1][1] == arr[2][0]) && (arr[1][1] != ' '))
return arr[1][1];
}
return ' ';
}
void player_turn(char arr[3][3])
{
int x = 0;
int y = 0;
while (1)
{
printf("Select the position to play (enter the corresponding x,y coordinates):>");
scanf_s("%d,%d", &x, &y);
if (arr[--x][--y] == ' ')
{
arr[x][y] = 'X';
break;
}
else
{
printf("There is already a piece in that position, please reselect it.\n");
}
}
}
void computer_turn(char arr[3][3])
{
srand((unsigned)time(NULL));
while (1)
{
int x = rand() % 3;
int y = rand() % 3;
if (arr[x][y] == ' ')
{
arr[x][y] = 'O';
break;
}
}
}
int main()
{
int input = 0;
int a = 0;
int b = 0;
char arr[3][3] = { 0 };
do
{
menu();
printf("Please select the action you want to perform:>");
scanf_s("%d", &input);
switch (input)
{
case 1:
init_chess(arr);
if (whom_act())
{
while (1)
{
computer_act(arr);
print_chess(arr);
if ((jud_win(arr)) == 'X')
{
printf("You won the competition!\n");
a++;
break;
}
else if ((jud_win(arr)) == 'O')
{
printf("You lose!\n");
b++;
break;
}
player_act(arr);
print_chess(arr);
if ((jud_win(arr)) == 'X')
{
printf("You won the competition!\n");
a++;
break;
}
else if ((jud_win(arr)) == 'O')
{
printf("You lose!\n");
b++;
break;
}
else if (chess_full(arr))
{
printf("Tie!\n");
break;
}
}
}
else
{
while (1)
{
print_chess(arr);
player_act(arr);
print_chess(arr);
if ((jud_win(arr)) == 'X')
{
printf("You won the competition!\n");
a++;
break;
}
else if ((jud_win(arr)) == 'O')
{
printf("You lose!\n");
b++;
break;
}
computer_act(arr);
print_chess(arr);
if ((jud_win(arr)) == 'X')
{
printf("You won the competition!\n");
a++;
break;
}
else if ((jud_win(arr)) == 'O')
{
printf("You lose!\n");
b++;
break;
}
else if (chess_full(arr))
{
printf("Tie!\n");
break;
}
}
}
break;
case 0:
printf("The numbers of wins are %d ",a);
printf("The numbers of losses are %d ",b);
printf("Exiting the game\n");
break;
}
} while (input);
return 0;
}
井字棋游戏可参考这个,十分详细,配套资源和视频
http://t.csdn.cn/jvrmL
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<ctime>
using namespace std;
char play[9] = { '1','2','3','4','5','6','7','8','9' };
void system()
{
system("color FD");
system("date/t");
system("time/t");
system("title 井字棋 Ver:1.0.0");
}
void CSH()
{
cout << "######井字棋小游戏######" << endl;
cout << "(*游戏规则:先掷骰子选出优先下子者,然后轮流下子,任一方若有三子连成一线,该方即获胜)" << endl;
cout << "+---+---+---+\n"
"| " << play[0] << " | " << play[1] << " | " << play[2] << " |\n"
"+---+---+---+\n"
"| " << play[3] << " | " << play[4] << " | " << play[5] << " |\n"
"+---+---+---+\n"
"| " << play[6] << " | " << play[7] << " | " << play[8] << " |\n"
"+---+---+---+\n"<<endl;
cout<<"鸡你太美"<<endl;
cout<<"你干嘛,哎呦"<<endl;
cout<<"-------------------------------------------------------"<<endl;
}
void return_CSH()
{
memset(play,0,9);
play[0] = '1';
play[1] = '2';
play[2] = '3';
play[3] = '4';
play[4] = '5';
play[5] = '6';
play[6] = '7';
play[7] = '8';
play[8] = '9';
}
class Player {
public:
Player() {}
void Name(int i)
{
cout << "请为玩家" << i << "设置玩家名称:";
cin >> name;
}
void Get_Name()
{
cout << name;
}
int Order()
{
cout << "请玩家 " << name << " 掷骰子:";
system("pause");
srand((unsigned)time(NULL));
a = rand() % 6 + 1;
cout << a << endl;
return a;
}
int PD()
{
return a;
}
void XQ_1()
{
cout << "******游戏设置******" << endl;
cout << " 1.O 2.X " << endl;
cout << "请玩家 " << name << " 选择执什么棋(1 或者 2):";
while (xq == 0)
{
cin >> xq;
if (xq == 1)
q1 = 'O';
else if (xq == 2)
q1 = 'X';
else
cout << "命令错误!请重新输入合法选项:";
}
}
void XQ_2()
{
cout << "请玩家 " << name << " 选择执什么棋(1 或者 2):";
while (xq == 0)
{
cin >> xq;
if (xq == 1)
q2 = 'O';
else if (xq == 2)
q2 = 'X';
else
cout << "命令错误!请重新输入合法选项:";
}
}
void XS_Play()
{
int n;
cout << "请玩家 " << name << " 输入下棋位置:";
cin >> n;
if (play[n - 1] != 'O' && play[n - 1] != 'X'&&n<10)
{
play[n - 1] = q1;
}
else
{
cout << "位置不合法!请重新选择:";
cin >> n;
if (play[n - 1] != 'O' && play[n - 1] != 'X' && n < 10)
{
play[n - 1] = q1;
}
else
{
cout << "我感觉你是故意的";
exit(1);
}
}
system("cls");
cout << "+---+---+---+\n"
"| " << play[0] << " | " << play[1] << " | " << play[2] << " |\n"
"+---+---+---+\n"
"| " << play[3] << " | " << play[4] << " | " << play[5] << " |\n"
"+---+---+---+\n"
"| " << play[6] << " | " << play[7] << " | " << play[8] << " |\n"
"+---+---+---+\n";
}
void HS_Play()
{
int n;
cout << "请玩家 " << name << " 输入下棋位置:";
cin >> n;
if (play[n - 1] != 'O' && play[n - 1] != 'X' && n < 10)
{
play[n - 1] = q2;
}
else
{
cout << "位置不合法!请重新选择:";
cin >> n;
if (play[n - 1] != 'O' && play[n - 1] != 'X' && n < 10)
{
play[n - 1] = q2;
}
else
{
cout << "你又找茬是吧?";
exit(1);
}
}
system("cls");
cout << "+---+---+---+\n"
"| " << play[0] << " | " << play[1] << " | " << play[2] << " |\n"
"+---+---+---+\n"
"| " << play[3] << " | " << play[4] << " | " << play[5] << " |\n"
"+---+---+---+\n"
"| " << play[6] << " | " << play[7] << " | " << play[8] << " |\n"
"+---+---+---+\n";
}
friend int Get_Q1(Player& p1);
friend int Get_Q2(Player& p2);
private:
string name;
int a = 0;
int xq = 0;
char q1, q2;
};
int Get_Q1(Player& p1)
{
return p1.q1;
}
int Get_Q2(Player& p2)
{
return p2.q2;
}
int End()
{
if (play[0] == play[1] && play[1] == play[2] && play[0] != ' ')
{
if (play[0] == 'X')
{
return 1;
}
if (play[0] == 'O')
{
return 2;
}
}
if (play[3] == play[4] && play[4] == play[5] && play[3] != ' ')
{
if (play[3] == 'X')
{
return 1;
}
if (play[3] == 'O')
{
return 2;
}
}
if (play[6] == play[7] && play[7] == play[8] && play[6] != ' ')
{
if (play[6] == 'X')
{
return 1;
}
if (play[6] == 'O')
{
return 2;
}
}
if (play[0] == play[3] && play[3] == play[6] && play[0] != ' ')
{
if (play[0] == 'X')
{
return 1;
}
if (play[0] == 'O')
{
return 2;
}
}
if (play[1] == play[4] && play[4] == play[7] && play[1] != ' ')
{
if (play[1] == 'X')
{
return 1;
}
if (play[1] == 'O')
{
return 2;
}
}
if (play[2] == play[5] && play[5] == play[8] && play[2] != ' ')
{
if (play[1] == 'X')
{
return 1;
}
if (play[1] == 'O')
{
return 2;
}
}
if (play[0] == play[4] && play[4] == play[8] && play[0] != ' ')
{
if (play[0] == 'X')
{
return 1;
}
if (play[0] == 'O')
{
return 2;
}
}
if (play[2] == play[4] && play[4] == play[6] && play[2] != ' ')
{
if (play[2] == 'X')
{
return 1;
}
if (play[2] == 'O')
{
return 2;
}
}
return 0;
}
int main()
{
system();
CSH();
Player p1;
Player p2;
int xz;
int j = 0;
int xs = 0;
p1.Name(1);
p2.Name(2);
while (xs == 0)
{
p1.Order();
p2.Order();
if (p1.PD() > p2.PD())
cout << "\n玩家 ", p1.Get_Name(), cout << " 先手\n" << endl, system("pause"), system("cls"), xs = 1;
else if (p1.PD() < p2.PD())
cout << "\n玩家 ", p2.Get_Name(), cout << " 先手\n" << endl, system("pause"), system("cls"), xs = 2;
else
cout << "双方点数一样大,请重新掷点数:" << endl;
}
if (xs == 1)
{
p1.XQ_1(), p2.XQ_2();
}
else if (xs == 2)
{
p2.XQ_1(), p1.XQ_2();
}
CSH();
while (j < 5)
{
if (xs == 1)
{
p1.XS_Play();
End();
if (End() != 0)
{
break;
}
p2.HS_Play();
End();
if (End() != 0)
{
break;
}
}
if (xs == 2)
{
p2.XS_Play();
End();
if (End() != 0)
{
break;
}
p1.HS_Play();
End();
if (End() != 0)
{
break;
}
}
j++;
}
system("cls");
cout<<"******游戏结束******"<<endl;
cout << "+---+---+---+\n"
"| " << play[0] << " | " << play[1] << " | " << play[2] << " |\n"
"+---+---+---+\n"
"| " << play[3] << " | " << play[4] << " | " << play[5] << " |\n"
"+---+---+---+\n"
"| " << play[6] << " | " << play[7] << " | " << play[8] << " |\n"
"+---+---+---+\n";
if (End() == 1)
{
if (Get_Q1(p1) == 'x')
cout << "\n玩家 ", p1.Get_Name(), cout << " 获得胜利!"<<endl;
else
cout << "\n玩家 ", p2.Get_Name(), cout << " 获得胜利!"<<endl;
}
if (End() == 2)
{
if (Get_Q1(p1) == 'O')
cout << "\n玩家 ", p1.Get_Name(), cout << " 获得胜利!"<<endl;
else
cout << "\n玩家 ", p2.Get_Name(), cout << " 获得胜利!"<<endl;
}
cout<<"------------------------------"<<endl;
cout<<"********是否继续游戏?********"<<endl;
cout<<"1.重新开始 2.退出游戏"<<endl;
cin>>xz;
switch(xz)
{
case 1:return_CSH(),main();break;
case 2:return 0;break;
default:cout<<"指令错误,游戏终止!";break;
}
}
可以参考一下:
https://blog.csdn.net/aiyubaobei/article/details/119488395
https://blog.csdn.net/crrrush/article/details/124739680
creat file()的函数是干嘛的,又没有返回值,打开文件后又没有文件操作
kk
代码加注释是个好习惯
代码没有贴全吧, computer_act 和 player_act 等函数都没有实现代码
可以参考一下:https://blog.csdn.net/wangdechang119/article/details/122298060
1.初始化棋盘;
2.输出棋盘;
3.玩家下棋;
4.检测棋盘;
5.电脑下棋;
6.检测棋盘
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <time.h>
#include <stdbool.h>
#include <stdlib.h>
#define START 1
#define QUIT 0
#define ROW 3
#define COL 3
static char chess_board[ROW][COL];//定义棋盘
void StartGame();
void InitGame();
void PrintfChess();
void PlayerMove();
void ComputerMove();
char CheckGameOver();
bool ChessFull();
int main(int argc, char* argv[])
{
int select = 1;
while (select)
{
printf("*********************\n");
printf("* [1] Start Game *\n");
printf("* [2] Over Game *\n");
printf("*********************\n");
printf("请选择:>");
scanf_s("%d", &select);
if (select == QUIT)
break;
if (select != START)
{
printf("输入有错,请重新输入.....\n");
continue;
}
StartGame();
}
printf("GoodBye.....");
return 0;
}
void StartGame()
{
char winner;
//1 初始化游戏(棋盘)
InitGame();
//2 进入游戏
while (1)
{
//3 输出棋盘
PrintfChess();
//4玩家下棋
PlayerMove();
//5检查结果
winner = CheckGameOver();
if (winner != 'c')
break;
//6电脑下棋
ComputerMove();
//7检查结果
CheckGameOver();
winner = CheckGameOver();
if (winner != 'c')
break;
}
if (winner == 'x')
printf("玩家赢.\n");
if (winner == 'o')
printf("电脑赢.\n");
if (winner == 'h')
printf("和棋.\n");
}
void InitGame()
{
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
chess_board[i][j] = ' ';
}
}
void PrintfChess()//输出棋盘,棋盘的设计可以根据自己的喜好设计
{
for (int i = 0; i < ROW; i++)
{
printf("| %c | %c | %c |\n", chess_board[i][0], chess_board[i][1], chess_board[i][2]);
if (i < ROW - 1)
printf("|---|---|---|\n");
}
}
void PlayerMove()//玩家下棋
{
printf("玩家落子.\n");
int row, col;
while (1)
{
printf("请输入一组坐标(下棋位置):>");
scanf("%d %d", &row, &col);
//检查坐标的有效性
if (row < 0 || row > ROW || col < 0 || col > COL)
{
printf("输入非法,请重新输入...");
continue;
}
if (chess_board[row][col] != ' ')
{
printf("输入的位置已被占用,请重新输入...");
continue;
}
chess_board[row][col] = 'x';//x代表玩家下的棋
break;
}
}
void ComputerMove()//电脑下棋
{
srand(time(0));
while (1)
{
int row = rand() % ROW;
int col = rand() % COL;
if (chess_board[row][col] != ' ')
{
continue;
}
chess_board[row][col] = 'o';//o代表电脑下的棋
break;
}
}
/*
* 'x'代表玩家赢
* 'o'代表电脑赢
* 'h'代表和棋
* 'c'代表继续
*/
char CheckGameOver()//检测游戏是否结束
{
//检查行
for (int i = 0; i < ROW; i++)
{
if (chess_board[i][0] != ' '
&& chess_board[i][0] == chess_board[i][1]
&& chess_board[i][0] == chess_board[i][2]
)
return chess_board[i][0];
}
//检查列
for (int j = 0; j < COL; j++)
{
if (chess_board[0][j] != ' '
&& chess_board[0][j] == chess_board[1][j]
&& chess_board[0][j] == chess_board[2][j]
)
return chess_board[0][j];
}
//检查对角线
if (chess_board[0][0] != ' '
&& chess_board[0][0] == chess_board[1][1]
&& chess_board[0][0] == chess_board[2][2]
)
return chess_board[0][0];
if (chess_board[0][2] != ' '
&& chess_board[0][2] == chess_board[1][1]
&& chess_board[0][2] == chess_board[2][0]
)
return chess_board[0][2];
//判断是否和棋
if (ChessFull())
return 'h';
return 'c';
}
bool ChessFull()
{
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
if (chess_board[i][j] == ' ')
return false;
}
}
return true;
}