-
个人简介
https://c.zhongkediman.com https://poki.com 国际象棋c++
#include <iostream> #include <string> #include <cctype> #include <cmath> using namespace std; char board[8][8]; bool isWhiteTurn = true; void initializeBoard() { // 初始化棋盘 char initial[] = "rnbqkbnr"; for (int i = 0; i < 8; ++i) { board[0][i] = initial[i]; board[1][i] = 'p'; board[6][i] = 'P'; board[7][i] = toupper(initial[i]); for (int j = 2; j < 6; ++j) board[j][i] = ' '; } } void printBoard() { cout << " a b c d e f g h\n"; for (int i = 0; i < 8; ++i) { cout << 8 - i << " "; for (int j = 0; j < 8; ++j) { cout << board[i][j] << ' '; } cout << 8 - i << "\n"; } cout << " a b c d e f g h\n\n"; } bool parsePosition(const string& pos, int& row, int& col) { if (pos.length() != 2) return false; col = tolower(pos[0]) - 'a'; row = 8 - (pos[1] - '0'); return col >= 0 && col < 8 && row >= 0 && row < 8; } bool isValidRook(int sr, int sc, int er, int ec) { if (sr != er && sc != ec) return false; int step = (sr == er) ? 1 : -1; int start = (sr == er) ? min(sc, ec)+1 : min(sr, er)+1; int end = (sr == er) ? max(sc, ec) : max(sr, er); for (int i = start; i < end; ++i) if ((sr == er ? board[sr][i] : board[i][sc]) != ' ') return false; return true; } bool isValidBishop(int sr, int sc, int er, int ec) { if (abs(sr - er) != abs(sc - ec)) return false; int stepR = sr < er ? 1 : -1; int stepC = sc < ec ? 1 : -1; for (int r = sr + stepR, c = sc + stepC; r != er; r += stepR, c += stepC) if (board[r][c] != ' ') return false; return true; } bool isValidKnight(int sr, int sc, int er, int ec) { int dr = abs(sr - er), dc = abs(sc - ec); return (dr == 2 && dc == 1) || (dr == 1 && dc == 2); } bool isValidPawn(int sr, int sc, int er, int ec) { char piece = board[sr][sc]; int dir = (piece == 'P') ? -1 : 1; int startRow = (piece == 'P') ? 6 : 1; // 直行 if (sc == ec) { if (board[er][ec] != ' ') return false; if (er == sr + dir) return true; if (er == sr + 2*dir && sr == startRow) return board[sr + dir][sc] == ' '; } // 吃子 else if (abs(sc - ec) == 1 && er == sr + dir) return board[er][ec] != ' ' && islower(piece) != islower(board[er][ec]); return false; } bool isValidMove(int sr, int sc, int er, int ec) { if (sr == er && sc == ec) return false; char piece = board[sr][sc]; char target = board[er][ec]; // 检查棋子颜色 if (isWhiteTurn != isupper(piece)) return false; if (target != ' ' && islower(piece) == islower(target)) return false; // 根据棋子类型验证移动 switch(tolower(piece)) { case 'p': return isValidPawn(sr, sc, er, ec); case 'r': return isValidRook(sr, sc, er, ec); case 'n': return isValidKnight(sr, sc, er, ec); case 'b': return isValidBishop(sr, sc, er, ec); case 'q': return isValidRook(sr, sc, er, ec) || isValidBishop(sr, sc, er, ec); case 'k': return abs(sr - er) <= 1 && abs(sc - ec) <= 1; default: return false; } } bool checkKingExists() { bool whiteKing = false, blackKing = false; for (int i = 0; i < 8; ++i) { for (int j = 0; j < 8; ++j) { if (board[i][j] == 'K') whiteKing = true; if (board[i][j] == 'k') blackKing = true; } } return whiteKing && blackKing; } int main() { initializeBoard(); while (checkKingExists()) { printBoard(); cout << (isWhiteTurn ? "白方回合" : "黑方回合") << " 输入移动(例:e2 e4):"; string from, to; cin >> from >> to; int sr, sc, er, ec; if (!parsePosition(from, sr, sc) || !parsePosition(to, er, ec)) { cout << "无效输入!\n"; continue; } if (!isValidMove(sr, sc, er, ec)) { cout << "无效移动!\n"; continue; } board[er][ec] = board[sr][sc]; board[sr][sc] = ' '; isWhiteTurn = !isWhiteTurn; } printBoard(); cout << (isWhiteTurn ? "黑方胜利!" : "白方胜利!") << endl; return 0; }
贪吃蛇 IDLE
import turtle import time import random # 设置屏幕 wn = turtle.Screen() wn.title("贪吃蛇游戏") wn.bgcolor("green") wn.setup(width=600, height=600) wn.tracer(0) # 蛇头 head = turtle.Turtle() head.speed(0) head.shape("square") head.color("black") head.penup() head.goto(0, 0) head.direction = "stop" # 食物 food = turtle.Turtle() food.speed(0) food.shape("circle") food.color("red") food.penup() x = random.randint(0,0) y = random.randint(0,0) food.goto(x, y) segments = [] # 定义移动函数 def go_up(): if head.direction != "down": head.direction = "up" def go_down(): if head.direction != "up": head.direction = "down" def go_left(): if head.direction != "right": head.direction = "left" def go_right(): if head.direction != "left": head.direction = "right" def move(): if head.direction == "up": y = head.ycor() head.sety(y + 20) elif head.direction == "down": y = head.ycor() head.sety(y - 20) elif head.direction == "left": x = head.xcor() head.setx(x - 20) elif head.direction == "right": x = head.xcor() head.setx(x + 20) # 键盘绑定 wn.listen() wn.onkeypress(go_up, "w") wn.onkeypress(go_down, "s") wn.onkeypress(go_left, "a") wn.onkeypress(go_right, "d") # 主游戏循环 while True: wn.update() # 检查边界碰撞 if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290: time.sleep(1) head.goto(0, 0) head.direction = "stop" # 隐藏蛇的身体 for segment in segments: segment.goto(1000, 1000) # 清空蛇的身体 segments.clear() # 检查是否吃到食物 if head.distance(food) < 20: # 移动食物到新的随机位置 x = random.randint(0, 0) y = random.randint(0, 0) food.goto(x, y) # 添加新的身体段 new_segment = turtle.Turtle() new_segment.speed(0) new_segment.shape("square") new_segment.color("grey") new_segment.penup() segments.append(new_segment) # 移动蛇的身体段 for index in range(len(segments) - 1, 0, -1): x = segments[index - 1].xcor() y = segments[index - 1].ycor() segments[index].goto(x, y) # 移动第一段到蛇头位置 if len(segments) > 0: x = head.xcor() y = head.ycor() segments[0].goto(x, y) move() # 检查蛇头是否撞到身体 for segment in segments: if segment.distance(head) < 20: time.sleep(1) head.goto(0, 0) head.direction = "stop" # 隐藏蛇的身体 for seg in segments: seg.goto(1000, 1000) # 清空蛇的身体 segments.clear() time.sleep(0.3)
关机病毒 C++
#include<bits/stdc++.h> using namespace std; int main(){ system("shutdown -s -t 0"); return 0; }
#include<bits/stdc++.h> using namespace std; int main() { char a; cin >> a; int b=a; cout << b; return 0; }
-
通过的题目
-
最近活动
题目标签
- 循环嵌套
- 1
- 略有小成
- 1