做点有意思的
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
1.打印下列图形 | 1 2 3| |1 | |2 | |3 | | 1 2 3| |1 x x x| |2 x x x| |3 x x x| | 1 2 3| |1 o o o| |2 o o o| |3 o o o| 2.在1的基础上,根据输入的坐标(x,y)打印图形 超出范围提示输入错误 例如输入(2,3) 则打印 | 1 2 3| |1 | |2 | |3 x | 3.在2的基础上,输入坐标后再输入x或o,在对应位置打印 超出范围提示输入错误 输入的不是x或o,提示错误 4.先打印空棋盘,再重复过程3,每次清屏 超出范围提示输入错误 输入的不是x或o,提示错误 输入的坐标已经有内容了,提示错误 提示:清屏操作如下 #include<windows.h> system("cls"); 5.(meduim)在4的基础上,增加规则判定 先手x后,后手必须是o,否则报错 当横竖斜满足都是x或都是o后,提示x方或o方获胜,程序退出 6.(hard)升级版,在上述基础之上,要求可存储进度和恢复上一次进度 |
代码如下,每变动一次重新粘贴一版,方便你们观察变化
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#include <stdio.h> // http://blog.hylstudio.cn/archives/690 // | 1 2 3| // |1 | // |2 | // |3 | // | 1 2 3| // |1 x x x| // |2 x x x| // |3 x x x| // | 1 2 3| // |1 o o o| // |2 o o o| // |3 o o o| int main() { printf("| 1 2 3|\n"); printf("|1 |\n"); printf("|2 |\n"); printf("|3 |\n"); printf("\n"); printf("| 1 2 3|\n"); printf("|1 x x x|\n"); printf("|2 x x x|\n"); printf("|3 x x x|\n"); printf("\n"); printf("| 1 2 3|\n"); printf("|1 o o o|\n"); printf("|2 o o o|\n"); printf("|3 o o o|\n"); printf("\n"); return 0; } |
第二题
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
#include <stdio.h> // http://blog.hylstudio.cn/archives/690 // 在1的基础上,根据输入的坐标(x,y)打印图形 // 超出范围提示输入错误 // 例如输入2,3 // 则打印 // | 1 2 3| // |1 | // |2 x| // |3 | int main() { int row = 1; int column = 1; int i = 0; int j = 0; int boardColumns = 3; int boardRows = 3; int board[][3] = { {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, }; int chess = 0; char chessChar = ' '; printf("please input position:"); scanf("%d,%d", &row, &column); board[row - 1][column - 1] = -1; if (row < 1 || column < 1 || row > boardRows || column > boardColumns) { printf("input position error"); return -1; } // print chessboard head printf("| 1 2 3|\n"); for (i = 0; i < boardRows; i++) { printf("|"); printf("%d", i + 1); for (j = 0; j < boardColumns; j++) { chess = board[i][j]; if (chess == 0) { chessChar = ' '; } else if (chess == 1) { chessChar = 'o'; } else if (chess == -1) { chessChar = 'x'; } printf(" %c", chessChar); } printf("|\n"); } printf("\n"); return 0; } |
第三题
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
#include <stdio.h> // http://blog.hylstudio.cn/archives/690 // 3.在2的基础上,输入坐标后再输入x或o,在对应位置打印 // 超出范围提示输入错误 // 输入的不是x或o,提示错误 int main() { int row = 1; int column = 1; int i = 0; int j = 0; int boardColumns = 3; int boardRows = 3; int board[][3] = { {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, }; int chess = 0; char chessChar = ' '; printf("please input position:"); scanf("%d,%d", &row, &column); if (row < 1 || column < 1 || row > boardRows || column > boardColumns) { printf("input position error"); return -1; } printf("please input chess:"); scanf("\n%c", &chessChar);//这里多写一个\n是为了处理输入完位置之后的回车键 if (chessChar == 'x') { board[row - 1][column - 1] = -1; } else if (chessChar == 'o') { board[row - 1][column - 1] = 1; } else { printf("input chess error"); return -1; } // print chessboard head printf("| 1 2 3|\n"); for (i = 0; i < boardRows; i++) { printf("|"); printf("%d", i + 1); for (j = 0; j < boardColumns; j++) { chess = board[i][j]; if (chess == 0) { chessChar = ' '; } else if (chess == 1) { chessChar = 'o'; } else if (chess == -1) { chessChar = 'x'; } printf(" %c", chessChar); } printf("|\n"); } printf("\n"); return 0; } |
第四题
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
#include <stdio.h> #include <windows.h> // 4.先打印空棋盘,再重复过程3,每次清屏 // 超出范围提示输入错误 // 输入的不是x或o,提示错误 // 输入的坐标已经有内容了,提示错误 // 提示:清屏操作如下 // #include<windows.h> // system("cls"); int main() { int row = 1; int column = 1; int i = 0; int j = 0; int boardColumns = 3; int boardRows = 3; int board[][3] = { {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, }; int chess = 0; char chessChar = ' '; while (1) { printf("please input position:"); scanf("%d,%d", &row, &column); if (row < 1 || column < 1 || row > boardRows || column > boardColumns) { printf("input position error\n"); continue; } if (board[row - 1][column - 1] != 0) { printf("input position erro, already has chess\n"); continue; } printf("please input chess:"); scanf("\n%c", &chessChar); //这里多写一个\n是为了处理输入完位置之后的回车键 if (chessChar == 'x') { board[row - 1][column - 1] = -1; } else if (chessChar == 'o') { board[row - 1][column - 1] = 1; } else { printf("input chess error\n"); continue; } system("cls");//vscode下debug有问题,需要debug的话暂时去掉这行 // print chessboard head printf("| 1 2 3|\n"); for (i = 0; i < boardRows; i++) { printf("|"); printf("%d", i + 1); for (j = 0; j < boardColumns; j++) { chess = board[i][j]; if (chess == 0) { chessChar = ' '; } else if (chess == 1) { chessChar = 'o'; } else if (chess == -1) { chessChar = 'x'; } printf(" %c", chessChar); } printf("|\n"); } printf("\n"); } return 0; } |
第五题
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
#include <stdio.h> #include <windows.h> // 5.(meduim)在4的基础上,增加规则判定 // 先手x后,后手必须是o,否则报错 // 当横竖斜满足都是x或都是o后,提示x方或o方获胜,程序退出 int main() { int row = 1; int column = 1; int i = 0; int j = 0; int boardColumns = 3; int boardRows = 3; int board[][3] = { {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, }; int chess = 0; char chessChar = ' '; char lastChessChar = ' '; int winFlag = 0; int firstInput = 1; while (1) { printf("please input position:"); scanf("%d,%d", &row, &column); getchar();//处理掉回车 if (row < 1 || column < 1 || row > boardRows || column > boardColumns) { printf("input position error\n"); continue; } if (board[row - 1][column - 1] != 0) { printf("input position erro, already has chess\n"); continue; } printf("please input chess:"); scanf("%c", &chessChar); getchar();//处理掉回车 if ((firstInput == 1 && chessChar == 'x') || (chessChar == 'x' && lastChessChar == 'o')) { board[row - 1][column - 1] = -1; } else if ((firstInput == 1 && chessChar == 'o') || (chessChar == 'o' && lastChessChar == 'x')) { board[row - 1][column - 1] = 1; } else { printf("input chess error\n"); continue; } if (firstInput == 1) { firstInput = 0; } lastChessChar = chessChar; system("cls"); // vscode下debug有问题,需要debug的话暂时去掉这行 // print chessboard head printf("| 1 2 3|\n"); for (i = 0; i < boardRows; i++) { printf("|"); printf("%d", i + 1); for (j = 0; j < boardColumns; j++) { chess = board[i][j]; if (chess == 0) { chessChar = ' '; } else if (chess == 1) { chessChar = 'o'; } else if (chess == -1) { chessChar = 'x'; } printf(" %c", chessChar); } printf("|\n"); } printf("\n"); //判断规则是否有胜利的 //按行 for (i = 0; i < boardRows; i++) { winFlag = board[i][0] + board[i][1] + board[i][2]; if (winFlag == 3) { printf("o win"); return 0; } else if (winFlag == -3) { printf("x win"); return 0; } } //按列 for (j = 0; j < boardColumns; j++) { winFlag = board[0][j] + board[1][j] + board[2][j]; if (winFlag == 3) { printf("o win"); return 0; } else if (winFlag == -3) { printf("x win"); return 0; } } //斜着的 winFlag = board[0][0] + board[1][1] + board[2][2]; if (winFlag == 3) { printf("o win"); return 0; } else if (winFlag == -3) { printf("x win"); return 0; } winFlag = board[0][0] + board[1][1] + board[2][2]; if (winFlag == 3) { printf("o win"); return 0; } else if (winFlag == -3) { printf("x win"); return 0; } } return 0; } |
0 Comments