標(biāo)題:
有關(guān)c語(yǔ)言的三子棋+學(xué)生管理系統(tǒng)的小項(xiàng)目游戲源程序
[打印本頁(yè)]
作者:
久伴i
時(shí)間:
2021-7-1 17:28
標(biāo)題:
有關(guān)c語(yǔ)言的三子棋+學(xué)生管理系統(tǒng)的小項(xiàng)目游戲源程序
學(xué)單片機(jī)就必須會(huì)c語(yǔ)言,那些做個(gè)小游戲來(lái)檢驗(yàn)一下你的c語(yǔ)言水平吧
C語(yǔ)言源程序如下:
#include <stdio.h>
void printf_cds(int (*array)[3]); //打印UI
int input(int cds, int (*pcds)[3], int game); //獲取輸入
int scan_arry(int (*pcds)[3], int gamer); //掃描數(shù)組
int scan_arry_row(int (*pcds)[3], int gamer); //掃描行
int scan_arry_column(int (*pcds)[3], int gamer); //掃描列
int scan_arry_x(int (*pcds)[3], int gamer); //掃描對(duì)角線
int scan_arry_zero(int (*pcds)[3]);//掃描剩余位置
int main(int argc, char **argv)
{
int coords[3][3] = {{0, 0, 0},
{0, 0, 0},
{0, 0, 0}}; //第一個(gè)玩家用 1表示,第二個(gè)玩家用 2表示
int gamer01 = 10, who = 1;
char button;
//開(kāi)始界面
printf("游戲開(kāi)始\n");
printf("操作輸入數(shù)字鍵盤對(duì)應(yīng)的位置\n");
printf(" 7 | 8 | 9 \n");
printf("———————— ——————— ———————\n");
printf(" 4 | 5 | 6 \n");
printf("———————— ——————— ———————\n");
printf(" 1 | 2 | 3 \n\n");
printf_cds(coords);
while (1)
{
if (scan_arry_zero(coords) == 0)
return 0;
printf("請(qǐng)第%d位玩家輸入\n", who);
scanf("%d", &gamer01);
if (gamer01 != 0)
{
if (who == 1)
{
if (input(gamer01, coords, 1))
who = 2;
printf_cds(coords);
if (scan_arry(coords, 1) == 1)
{
printf("玩家%d勝利!", 1);
return 1;
}
}
else if (who == 2)
{
if (input(gamer01, coords, 2))
who = 1;
printf_cds(coords);
if (scan_arry(coords, 2) == 2)
{
printf("玩家%d勝利!", 2);
return 1;
}
}
}
else //結(jié)束游戲
{
printf("是否退出游戲?(y:是,n:否)\n");
getchar();
scanf("%c", &button);
getchar();
if (button == 'y')
return 0;
else if (button == 'n')
printf_cds(coords);
}
}
}
//掃描零值
int scan_arry_zero(int (*pcds)[3])
{
int i, j, count = 0;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
if (pcds[i][j] == 0)
count++;
}
}
if (count == 0)
{
printf("游戲結(jié)束,平局!");
return 0;
}
else
return 1;
}
//掃描數(shù)組
int scan_arry(int (*pcds)[3], int gamer)
{
if (scan_arry_row(pcds, gamer) == gamer)
return gamer;
else if (scan_arry_column(pcds, gamer) == gamer)
return gamer;
else if (scan_arry_x(pcds, gamer) == gamer)
return gamer;
else
return 0;
}
//掃描行
int scan_arry_row(int (*pcds)[3], int gamer)
{
int i, j, count_r = 0;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
if (pcds[i][j] == gamer)
{
count_r += pcds[i][j]; //獲取行數(shù)據(jù)
}
}
if (count_r / 3 == gamer)
return gamer;
else
count_r = 0;
}
return 0;
}
//掃描列
int scan_arry_column(int (*pcds)[3], int gamer)
{
int i, j, count_c = 0;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
if (pcds[j][i] == gamer)
count_c += pcds[j][i]; //獲取列數(shù)據(jù)
}
if (count_c / 3 == gamer)
return gamer;
else
count_c = 0;
}
return 0;
}
//掃描對(duì)角線
int scan_arry_x(int (*pcds)[3], int gamer)
{
int i, j, count_1 = 0, count_2 = 0;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
if (pcds[i][j] == gamer)
{
if (i == j) //獲取斜邊
count_1 += pcds[i][j];
if (i + j == 2) //獲取斜邊2
count_2 += pcds[i][j];
}
}
}
if (count_1 / 3 == gamer || count_2 / 3 == gamer)
return gamer;
else
return 0;
}
//獲取玩家輸入
int input(int cds, int (*pcds)[3], int gamer)
{
int arry[3][3] = {{7, 8, 9},
{4, 5, 6},
{1, 2, 3}};
int i, j;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
if (cds == arry[i][j])
{
if (pcds[i][j] == 0)
{
pcds[i][j] = gamer;
return 1;
}
else
{
printf("此位置已有數(shù)據(jù),請(qǐng)重新輸入\n");
return 0;
}
}
}
}
}
//打印游戲界面
void printf_cds(int (*array)[3])
{
int i, j;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf("%d\t", array[i][j]);
}
printf("\n\n");
}
}
復(fù)制代碼
/*做一個(gè)小項(xiàng)目:(簡(jiǎn)單的學(xué)生信息管理系統(tǒng)--》可以管理10個(gè)學(xué)生的信息)
定義一個(gè)結(jié)構(gòu)體數(shù)據(jù)類型,里面包括學(xué)生信息:
姓名、性別、學(xué)號(hào)、年齡、電話號(hào)碼、成績(jī)(語(yǔ)數(shù)英體),
可以自行選擇操作的增加、刪除、更改、查詢學(xué)生的信息等功能
*/
#include <stdio.h>
#include <strings.h>
#include <string.h>
int len = 10; //要管理的學(xué)生人數(shù) ------------測(cè)試用,完成功能后改回項(xiàng)目要求
//成績(jī) 的結(jié)構(gòu)體數(shù)據(jù)類型定義
struct score
{
int Chinese;
int Math;
int English;
int PE;
};
//學(xué)生 的 結(jié)構(gòu)體數(shù)據(jù)類型定義
struct student
{
char name[20];
char sex;
int ID;
unsigned char age;
unsigned long long phone_num;
struct score student_sc;
};
//函數(shù)聲明
void printf_UI(void);
void add_data(struct student *p_data);
void printf_data(struct student *p_data);
void delete_data(struct student *p_data);
void change_data(struct student *p_data);
int main(int argc, char **argv)
{
int cmd = 0;
struct student yq[len];
bzero(yq, sizeof(yq));
while (1)
{
printf_UI();
scanf("%d", &cmd);
if (cmd == 1)
add_data(yq);
else if (cmd == 2)
printf_data(yq);
else if (cmd == 3)
change_data(yq);
else if (cmd == 4)
delete_data(yq);
}
return 0;
}
//修改學(xué)生數(shù)據(jù)
void change_data(struct student *p_data)
{
int i, num=0;
for (i = 0; i < len; i++)
{
printf("學(xué)生%d姓名:%s\n", i + 1, p_data[i].name);
}
printf("\n請(qǐng)輸入需要修改的學(xué)生(1~10)\n");
scanf("%d", &num);
if(num<1||num>10)
{
printf("輸入有誤!\n");
return;
}
num -= 1;
printf("請(qǐng)輸入學(xué)生的姓名:");
scanf("%s", p_data[num].name);
getchar();
printf("請(qǐng)輸入學(xué)生的性別(M->男, W-->女):");
scanf("%c", &p_data[num].sex);
getchar();
printf("請(qǐng)輸入學(xué)生的學(xué)號(hào):");
scanf("%d", &p_data[num].ID);
printf("請(qǐng)輸入學(xué)生的年齡(0~255):");
scanf("%hhu", &p_data[num].age);
printf("請(qǐng)輸入學(xué)生的電話號(hào)碼:");
scanf("%llu", &p_data[num].phone_num);
printf("請(qǐng)輸入學(xué)生的語(yǔ)文成績(jī):");
scanf("%d", &p_data[num].student_sc.Chinese);
printf("請(qǐng)輸入學(xué)生的數(shù)學(xué)成績(jī):");
scanf("%d", &p_data[num].student_sc.Math);
printf("請(qǐng)輸入學(xué)生的英語(yǔ)成績(jī):");
scanf("%d", &p_data[num].student_sc.English);
printf("請(qǐng)輸入學(xué)生的體育成績(jī):");
scanf("%d", &p_data[num].student_sc.PE);
printf("\n");
printf("修改完成!\n\n");
}
//刪除數(shù)據(jù)
void delete_data(struct student *p_data)
{
int i;
for (i = 0; i < len; i++)
bzero(&p_data[i], sizeof(*p_data));
printf("數(shù)據(jù)已清除!\n\n");
}
//增加數(shù)據(jù)
void add_data(struct student *p_data)
{
int i;
for (i = 0; i < len; i++)
{
//姓名、性別、學(xué)號(hào)、年齡、電話號(hào)碼、成績(jī)(語(yǔ)數(shù)英體),
printf("請(qǐng)輸入學(xué)生的姓名:");
scanf("%s", p_data[i].name);
getchar();
printf("請(qǐng)輸入學(xué)生的性別(M->男, W-->女):");
scanf("%c", &p_data[i].sex);
getchar();
printf("請(qǐng)輸入學(xué)生的學(xué)號(hào):");
scanf("%d", &p_data[i].ID);
printf("請(qǐng)輸入學(xué)生的年齡(0~255):");
scanf("%hhu", &p_data[i].age);
printf("請(qǐng)輸入學(xué)生的電話號(hào)碼:");
scanf("%llu", &p_data[i].phone_num);
printf("請(qǐng)輸入學(xué)生的語(yǔ)文成績(jī):");
scanf("%d", &p_data[i].student_sc.Chinese);
printf("請(qǐng)輸入學(xué)生的數(shù)學(xué)成績(jī):");
scanf("%d", &p_data[i].student_sc.Math);
printf("請(qǐng)輸入學(xué)生的英語(yǔ)成績(jī):");
scanf("%d", &p_data[i].student_sc.English);
printf("請(qǐng)輸入學(xué)生的體育成績(jī):");
scanf("%d", &p_data[i].student_sc.PE);
printf("\n");
}
printf("數(shù)據(jù)輸入完成\n\n");
}
//打印數(shù)據(jù)
void printf_data(struct student *p_data)
{
int i;
for (i = 0; i < len; i++)
{
//姓名、性別、學(xué)號(hào)、年齡、電話號(hào)碼、成績(jī)(語(yǔ)數(shù)英體),
printf("學(xué)生%d姓名:%s\n", i + 1, p_data[i].name);
printf("學(xué)生%d性別:%c\n", i + 1, p_data[i].sex);
printf("學(xué)生%d學(xué)號(hào):%d\n", i + 1, p_data[i].ID);
printf("學(xué)生%d年齡:%hhu\n", i + 1, p_data[i].age);
……………………
…………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
51hei.png
(3.76 KB, 下載次數(shù): 76)
下載附件
2021-7-1 19:48 上傳
所有資料51hei提供下載:
三字棋游戲+學(xué)生簡(jiǎn)易管理系統(tǒng).7z
(9.64 KB, 下載次數(shù): 7)
2021-7-1 19:49 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1