求大神指导C语言 Readings:Chapter 6 (arrays)Problem:Write a C-progra
来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/09/03 17:57:06
求大神指导C语言
Readings:Chapter 6 (arrays)
Problem:Write a C-program for a game of Tic-Tac-Toe that is played between 2 players.You must define your own functions in your program.
Since the Tic-Tac-Toe board is like a 3×3 grid we will consider this board to have 9 positions as demonstrated by the following diagram.
|---|---|---|
| 1 | 2 | 3 |
|---|---|---|
| 4 | 5 | 6 |
|---|---|---|
| 7 | 8 | 9 |
|---|---|---|
Each player in turn places their symbol (an O or an X) on one of the 9 positions (by inputting in turn their choice of position).The game ends when one of the players places three O's or X's in a line (horizontal/vertical/diagonal) OR when all positions have been filled and neither player managed to place three O's or X's in a line (a draw).Player O goes first,and game board should be displayed at the end of each round (i.e.after a player places their symbol).
BONUS 2% may be scored by adding the following:
For the tic-tac-toe game
->Represent the board using a two-dimensional array
->Add the option of playing against the computer.Computer plays by randomly placing its marker on the board,i.e.involves no AI.
题目要求是用array 还有需要一个main function 和另外一个解决问题的function
Readings:Chapter 6 (arrays)
Problem:Write a C-program for a game of Tic-Tac-Toe that is played between 2 players.You must define your own functions in your program.
Since the Tic-Tac-Toe board is like a 3×3 grid we will consider this board to have 9 positions as demonstrated by the following diagram.
|---|---|---|
| 1 | 2 | 3 |
|---|---|---|
| 4 | 5 | 6 |
|---|---|---|
| 7 | 8 | 9 |
|---|---|---|
Each player in turn places their symbol (an O or an X) on one of the 9 positions (by inputting in turn their choice of position).The game ends when one of the players places three O's or X's in a line (horizontal/vertical/diagonal) OR when all positions have been filled and neither player managed to place three O's or X's in a line (a draw).Player O goes first,and game board should be displayed at the end of each round (i.e.after a player places their symbol).
BONUS 2% may be scored by adding the following:
For the tic-tac-toe game
->Represent the board using a two-dimensional array
->Add the option of playing against the computer.Computer plays by randomly placing its marker on the board,i.e.involves no AI.
题目要求是用array 还有需要一个main function 和另外一个解决问题的function
完整答案来了,自己写的,二维数组、对战、对ai都有,编译后,运行时带参数 pvaio表示ai用o(即ai先走),参数pvaie表示ai用x(人先走),参数pvp或不带参数表示人人对战,具体的自己看代码.#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#define TTT_FREE 0
#define TTT_O 1
#define TTT_X 2
#define TTT_TIE 3
/* 0:正常,1:O胜,2:X胜,3:平 */
int check_board(char board[3][3])
{
if(board[0][0] != TTT_FREE && board[0][0] == board[0][1] && board[0][0] == board[0][2])
return (board[0][0] == TTT_O)? TTT_O: TTT_X;
if(board[1][0] != TTT_FREE && board[1][0] == board[1][1] && board[1][0] == board[1][2])
return (board[1][0] == TTT_O)? TTT_O: TTT_X;
if(board[2][0] != TTT_FREE && board[2][0] == board[2][1] && board[2][0] == board[2][2])
return (board[2][0] == TTT_O)? TTT_O: TTT_X;
if(board[0][0] != TTT_FREE && board[0][0] == board[1][0] && board[0][0] == board[2][0])
return (board[0][0] == TTT_O)? TTT_O: TTT_X;
if(board[0][1] != TTT_FREE && board[0][1] == board[1][1] && board[0][1] == board[2][1])
return (board[0][1] == TTT_O)? TTT_O: TTT_X;
if(board[0][2] != TTT_FREE && board[0][2] == board[1][2] && board[0][2] == board[2][2])
return (board[0][2] == TTT_O)? TTT_O: TTT_X;
if(board[0][0] != TTT_FREE && board[0][0] == board[1][1] && board[0][0] == board[2][2])
return (board[0][0] == TTT_O)? TTT_O: TTT_X;
if(board[0][2] != TTT_FREE && board[0][2] == board[1][1] && board[0][2] == board[2][0])
return (board[0][2] == TTT_O)? TTT_O: TTT_X;
int i,j;
for(i=0; i<3; i++){
for(j=0; j<3; j++){
if(board[i][j] == TTT_FREE)
return TTT_FREE;
}
}
return TTT_TIE;
}
* 0:ok, 1:out of boundary 2:pos already in use */
int check_pos(char board[3][3], int pos)
{
if(pos > 9 || pos < 0)
return 1;
if(board[(pos-1)/3][(pos-1)%3] != TTT_FREE)
return 2;
return 0;
}
void print_board(char board[3][3]){
int i;
for(i=0; i<3; i++){
printf("%c %c %c\t%d %d %d\n",
(board[i][0] == TTT_FREE)? '#': ((board[i][0] == TTT_O)? 'O': 'X'),
(board[i][1] == TTT_FREE)? '#': ((board[i][1] == TTT_O)? 'O': 'X'),
(board[i][2] == TTT_FREE)? '#': ((board[i][2] == TTT_O)? 'O': 'X'),
i*3+1, i*3+2, i*3+3);
}
}
void ttt_start(int ai_player)
{
char board[3][3] = {0};
int cur_player = TTT_O;
print_board(board);
while(1){
int pos, ret;
if(ai_player != TTT_FREE && ai_player == cur_player){
srand(time(NULL));
while(1){
pos = rand()%9 + 1;
if(check_pos(board, pos) == 0)
break;
}
printf(" %s(AI) >>> %d\n", (ai_player == TTT_O)? "O": "X", pos);
} else {
printf(" %s >>> ", (cur_player == TTT_O)? "O": "X");
fflush(stdout);
scanf("%d", &pos);
if(check_pos(board, pos)){
printf(" err input, try again\n");
fflush(stdin);
continue;
}
}
board[(pos-1)/3][(pos-1)%3] = cur_player;
ret = check_board(board);
print_board(board);
if(ret != TTT_FREE){
if(ret == TTT_TIE){
printf("Game over, Tie\n");
} else {
printf("Game over, %s%s win\n", (ret==TTT_O)? "O": "X", (ai_player == ret)? "(AI)": "");
}
break;
}
cur_player = (cur_player == TTT_O)? TTT_X: TTT_O;
}
}
int main(int argc, char *argv[])
{
int ai_player = TTT_FREE;
if(argv[1]){
if(!strcasecmp(argv[1], "PvP")){
ai_player = TTT_FREE;
} else if(!strcasecmp(argv[1], "PvAIO")){
ai_player = TTT_O;
} else if(!strcasecmp(argv[1], "PvAIX")){
ai_player = TTT_X;
} else {
printf("Usage: exefile [PvP | PvAIO | PvAIX]\n");
return -1;
}
}
ttt_start(ai_player);
return 0;
}
再问: 大神 我是刚开始学C语言的学生 你写的这个有点复杂 有些语句没见过 你能否写个简单点的?
再答: 你只要看懂逻辑流程就行了, 对战就是 循环读取用户的输入->判断输入对不对->把输入更新到棋盘(更新数组的值)->检查棋盘(游戏是否结束) 电脑ai就是 在读取用户输入的时候用随机值来代替用户输入的值
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#define TTT_FREE 0
#define TTT_O 1
#define TTT_X 2
#define TTT_TIE 3
/* 0:正常,1:O胜,2:X胜,3:平 */
int check_board(char board[3][3])
{
if(board[0][0] != TTT_FREE && board[0][0] == board[0][1] && board[0][0] == board[0][2])
return (board[0][0] == TTT_O)? TTT_O: TTT_X;
if(board[1][0] != TTT_FREE && board[1][0] == board[1][1] && board[1][0] == board[1][2])
return (board[1][0] == TTT_O)? TTT_O: TTT_X;
if(board[2][0] != TTT_FREE && board[2][0] == board[2][1] && board[2][0] == board[2][2])
return (board[2][0] == TTT_O)? TTT_O: TTT_X;
if(board[0][0] != TTT_FREE && board[0][0] == board[1][0] && board[0][0] == board[2][0])
return (board[0][0] == TTT_O)? TTT_O: TTT_X;
if(board[0][1] != TTT_FREE && board[0][1] == board[1][1] && board[0][1] == board[2][1])
return (board[0][1] == TTT_O)? TTT_O: TTT_X;
if(board[0][2] != TTT_FREE && board[0][2] == board[1][2] && board[0][2] == board[2][2])
return (board[0][2] == TTT_O)? TTT_O: TTT_X;
if(board[0][0] != TTT_FREE && board[0][0] == board[1][1] && board[0][0] == board[2][2])
return (board[0][0] == TTT_O)? TTT_O: TTT_X;
if(board[0][2] != TTT_FREE && board[0][2] == board[1][1] && board[0][2] == board[2][0])
return (board[0][2] == TTT_O)? TTT_O: TTT_X;
int i,j;
for(i=0; i<3; i++){
for(j=0; j<3; j++){
if(board[i][j] == TTT_FREE)
return TTT_FREE;
}
}
return TTT_TIE;
}
* 0:ok, 1:out of boundary 2:pos already in use */
int check_pos(char board[3][3], int pos)
{
if(pos > 9 || pos < 0)
return 1;
if(board[(pos-1)/3][(pos-1)%3] != TTT_FREE)
return 2;
return 0;
}
void print_board(char board[3][3]){
int i;
for(i=0; i<3; i++){
printf("%c %c %c\t%d %d %d\n",
(board[i][0] == TTT_FREE)? '#': ((board[i][0] == TTT_O)? 'O': 'X'),
(board[i][1] == TTT_FREE)? '#': ((board[i][1] == TTT_O)? 'O': 'X'),
(board[i][2] == TTT_FREE)? '#': ((board[i][2] == TTT_O)? 'O': 'X'),
i*3+1, i*3+2, i*3+3);
}
}
void ttt_start(int ai_player)
{
char board[3][3] = {0};
int cur_player = TTT_O;
print_board(board);
while(1){
int pos, ret;
if(ai_player != TTT_FREE && ai_player == cur_player){
srand(time(NULL));
while(1){
pos = rand()%9 + 1;
if(check_pos(board, pos) == 0)
break;
}
printf(" %s(AI) >>> %d\n", (ai_player == TTT_O)? "O": "X", pos);
} else {
printf(" %s >>> ", (cur_player == TTT_O)? "O": "X");
fflush(stdout);
scanf("%d", &pos);
if(check_pos(board, pos)){
printf(" err input, try again\n");
fflush(stdin);
continue;
}
}
board[(pos-1)/3][(pos-1)%3] = cur_player;
ret = check_board(board);
print_board(board);
if(ret != TTT_FREE){
if(ret == TTT_TIE){
printf("Game over, Tie\n");
} else {
printf("Game over, %s%s win\n", (ret==TTT_O)? "O": "X", (ai_player == ret)? "(AI)": "");
}
break;
}
cur_player = (cur_player == TTT_O)? TTT_X: TTT_O;
}
}
int main(int argc, char *argv[])
{
int ai_player = TTT_FREE;
if(argv[1]){
if(!strcasecmp(argv[1], "PvP")){
ai_player = TTT_FREE;
} else if(!strcasecmp(argv[1], "PvAIO")){
ai_player = TTT_O;
} else if(!strcasecmp(argv[1], "PvAIX")){
ai_player = TTT_X;
} else {
printf("Usage: exefile [PvP | PvAIO | PvAIX]\n");
return -1;
}
}
ttt_start(ai_player);
return 0;
}
再问: 大神 我是刚开始学C语言的学生 你写的这个有点复杂 有些语句没见过 你能否写个简单点的?
再答: 你只要看懂逻辑流程就行了, 对战就是 循环读取用户的输入->判断输入对不对->把输入更新到棋盘(更新数组的值)->检查棋盘(游戏是否结束) 电脑ai就是 在读取用户输入的时候用随机值来代替用户输入的值
求大神指导C语言 Readings:Chapter 6 (arrays)Problem:Write a C-progra
求c语言大神指导 第二题!
求C语言大神指导第三题
C语言改错题,好急,求各路大神指导
求C语言大神指导!究竟怎么算长度?
求c语言大神指导!Problems:1) The factorial of a nonnegative integer
C语言求指导啊,
C语言题目,求思路求指导
c语言 Money Problem
C语言 Search Problem
C语言求max Problem DescriptionGiven a sequence a[1],a[2],a[3].a
求导大神指导C语言题目,我是新手,