用C + +的代码做出一个用鼠标点击的五子棋
时间: 2024-03-21 09:44:10 浏览: 64
++) {
if (strcmp(students[i].id, id) == 0) {
printf("请输入学生成绩:\好的,以下是用C++实现用鼠标点击的五子棋的代码示例:
```cpp
#include <iostreamn");
printf("C语言:");
scanf("%f", &students[i].score[0]);
printf("计算>
#include <graphics.h>
#include <conio.h>
const int BOARD_SIZE = 15;
const int CELL_SIZE = 40;
const物理学:");
scanf("%f", &students[i].score[1]);
printf("大学物理:");
int BOARD_OFFSET = CELL_SIZE / 2;
const int BOARD_WIDTH = CELL_SIZE * BOARD_SIZE;
const int BOARD_HEIGHT = CELL_SIZE * scanf("%f", &students[i].score[2]);
printf("原子物理:");
scanf("%f", &students[i BOARD_SIZE;
const int WINDOW_WIDTH = BOARD_WIDTH + CELL_SIZE;
const int WINDOW_HEIGHT = BOARD_HEIGHT + CELL_SIZE;
const int EMPTY =].score[3]);
printf("光学:");
scanf("%f", &students[i].score[4]);
printf(" 0;
const int BLACK = 1;
const int WHITE = 2;
int board[BOARD_SIZE][BOARD_SIZE] = { 0 };
int turn = BLACK;
void drawBoard()
{
setlinecolor(BLACK);
for (int i = 0;成功录入学生成绩!\n");
break;
}
}
if (i == studentNum) {
printf(" i < BOARD_SIZE; i++) {
line(BOARD_OFFSET, BOARD_OFFSET + i * CELL_SIZE, BOARD_OFFSET + BOARD_WIDTH, BOARD未找到学号为%s的学生!\n", id);
}
}
// 修改学生成绩
void modifyScore()_OFFSET + i * CELL_SIZE);
line(BOARD_OFFSET + i * CELL_SIZE, BOARD_OFFSET, BOARD_OFFSET + i * CELL_SIZE, {
printf("请输入要修改成绩的学生学号:");
char id[10];
scanf("%s", id);
BOARD_OFFSET + BOARD_HEIGHT);
}
}
void drawPiece(int row, int col, int color)
{
if (color == BLACK) int i;
for (i = 0; i < studentNum; i++) {
if (strcmp(students[i].id {
setfillcolor(BLACK);
setlinecolor(BLACK);
} else {
setfillcolor(WHITE);
, id) == 0) {
printf("请输入要修改的科目编号(1~5):");
int subject;
setlinecolor(WHITE);
}
fillcircle(BOARD_OFFSET + col * CELL_SIZE, BOARD_OFFSET + row * CELL_SIZE, CELL scanf("%d", &subject);
if (subject < 1 || subject > MAX_COURSE_NUM) {
printf("无_SIZE / 2 - 2);
}
bool checkWin(int row, int col, int color)
{
int count = 0;
效的科目编号!\n");
break;
}
printf("请输入修改后的成绩:");
scanf("% for (int i = row - 4; i <= row; i++) {
if (i < 0 || i + f", &students[i].score[subject - 1]);
printf("成功修改学生成绩!\n");
break;
4 >= BOARD_SIZE) {
continue;
}
count = 0;
for (int j = i; j < i + }
}
if (i == studentNum) {
printf("未找到学号为%s的学生!\n", 5; j++) {
if (board[j][col] == color) {
count++;
} else {
count = id);
}
}
// 删除学生成绩
void deleteScore() {
printf("请输入要删除成绩的学生学号0;
}
if (count == 5) {
return true;
}
}
}
for (int i =:");
char id[10];
scanf("%s", id);
int i;
for (i = 0; i < col - 4; i <= col; i++) {
if (i < 0 || i + 4 >= BOARD_SIZE) {
studentNum; i++) {
if (strcmp(students[i].id, id) == 0) {
printf("请输入要删除 continue;
}
count = 0;
for (int j = i; j < i + 5; j++) {
的科目编号(1~5):");
int subject;
scanf("%d", &subject);
if (subject < if (board[row][j] == color) {
count++;
} else {
count = 0;
}
if 1 || subject > MAX_COURSE_NUM) {
printf("无效的科目编号!\n");
break;
(count == 5) {
return true;
}
}
}
for (int i = row - 4, j = col - 4; i <= row && j <= col; i++, j++) {
if (i < 0 || j < }
students[i].score[subject - 1] = 0;
printf("成功删除学生成绩!\n");
break;
}
}
if (i == studentNum) {
printf("未找到学号为%s的学生! 0 || i + 4 >= BOARD_SIZE || j + 4 >= BOARD_SIZE) {
continue;
}
count = 0;
for (int k = 0; k < 5; k++) {
if (board[i + k][j +\n", id);
}
}
// 查询学生成绩
void showScore() {
printf("请输入要查询成绩的学 k] == color) {
count++;
} else {
count = 0;
}
if (count == 5)生学号:");
char id[10];
scanf("%s", id);
int i;
for (i = 0 {
return true;
}
}
}
for (int i = row - 4, j = col + 4;; i < studentNum; i++) {
if (strcmp(students[i].id, id) == 0) {
printf("C语言:%.1f\n", students[i].score[0]);
printf("计算物理学:%.1f\n i <= row && j >= col; i++, j--) {
if (i < 0 || j >= BOARD_SIZE || i + ", students[i].score[1]);
printf("大学物理:%.1f\n", students[i].score[2]);
4 >= BOARD_SIZE || j - 4 < 0) {
continue;
}
count = 0;
for (int printf("原子物理:%.1f\n", students[i].score[3]);
printf("光学:%.1f\n k = 0; k < 5; k++) {
if (board[i + k][j - k] == color) {
", students[i].score[4]);
break;
}
}
if (i == studentNum) {
printf("未找 count++;
} else {
count = 0;
}
if (count == 5) {
return true;
}
}
}
return false;
}
void handleClick(int x, int y)
{
int col = (x - BOARD_OFFSET到学号为%s的学生!\n", id);
}
}
// 查询学生总成绩
void showTotalScore() {
printf("学号\t姓名\t专业\t总成绩\n");
for (int i = 0; i <) / CELL_SIZE;
int row = (y - BOARD_OFFSET) / CELL_SIZE;
if (row >= BOARD_SIZE || col >= studentNum; i++) {
float totalScore = 0;
for (int j = 0; j < MAX_COURSE_NUM BOARD_SIZE || board[row][col] != EMPTY) {
return;
}
drawPiece(row, col, turn);
board[row; j++) {
totalScore += students[i].score[j];
}
printf("%s\t%s\t%s\t%.1f\n][col] = turn;
if (checkWin(row, col, turn)) {
std::cout << (turn == BLACK ? "",
students[i].id,
students[i].name,
students[i].major,
totalScore);
}
}
// 查询BLACK" : "WHITE") << " WIN" << std::endl;
getch();
closegraph();
exit(0);
}
学生单科成绩
void showSubjectScore() {
printf("请输入要查询的科目编号(1~5): turn = (turn == BLACK ? WHITE : BLACK);
}
int main()
{
initwindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Five");
int subject;
scanf("%d", &subject);
if (subject < 1 || subject > MAX_COURSE_NUM) {
in a Row");
drawBoard();
setbkcolor(WHITE);
while (true) {
if (ismouseclick(WM printf("无效的科目编号!\n");
return;
}
printf("学号\t姓名\t专业\t_LBUTTONDOWN)) {
int x = mousex();
int y = mousey();
handleClick(x, y);
}
}
%s成绩\n", subject == 1 ? "C语言" :
subject == 2 ? "计算物理学" return 0;
}
```
这段代码使用了EasyX图形库来实现窗口绘制和事件处理, :
subject == 3 ? "大学物理" :
subject == 4 ? "原子物理" :
"你可以根据自己的需求来选择相应的图形库或者操作系统API来实现。
阅读全文