五子棋

· · 休闲·娱乐

拥有与人机对战的功能,目前只开发出了 Easy 版本(但是 Easy 好像也很难打过)。

还有,更不动了,摆了。 ```cpp #include <bits/stdc++.h> #include <windows.h> #include <conio.h> using namespace std; const int len = 33; mt19937 thr(time(0)); int Dx[4] = { -1, 0, -1, -1 }, Dy[4] = { 0, -1, -1, 1 }; int ad[7][7][3], LX = 1, LY = 1; int nowc; void color(int x) { nowc = x; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x); } void HIDE() { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); // 获取控制台输出的句柄 CONSOLE_CURSOR_INFO cursorInfo; // 定义光标信息结构体变量 GetConsoleCursorInfo(hConsole, &cursorInfo); // 获取当前光标信息 cursorInfo.bVisible = FALSE; // 将光标设置为不可见 SetConsoleCursorInfo(hConsole, &cursorInfo); // 应用新的光标信息到控制台 } void init() { ad[2][2][0] = 5, ad[2][3][0] = 3; for (int i = 2; i <= 3; i++) ad[2][i][1] = ad[2][i][0], ad[2][i][2] = ad[2][i][1] + 5; ad[3][3][0] = 100, ad[3][4][0] = 80, ad[3][5][0] = 10, ad[3][5][1] = 800, ad[3][5][2] = 1000; for (int i = 3; i <= 4; i++) ad[3][i][1] = ad[3][i][0] + 4000, ad[3][i][2] = ad[3][i][1] + 1000; ad[4][4][0] = 2000, ad[4][5][0] = 2000, ad[4][4][1] = 5000, ad[4][5][1] = 4500, ad[4][4][2] = 1e8, ad[4][5][2] = 1e8, ad[4][5][2] = 1500; ad[5][5][0] = ad[5][5][1] = ad[5][5][2] = 3e8; } void gotoxy(short x, short y) { swap(x, y); x *= 2; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), { x, y }); } int N, c[len + 10][len + 10], C, s[len + 10][len + 10]; double Rand() { return 1.0 / (int)(thr() | 1) + 1; } void PRIORITY(int per, double p) { for (int i = 1; i <= len; i++) for (int j = 1; j <= len; j++) { if (c[i][j]) { s[i][j] += -1e9; continue; } for (int u = 0; u < 4; u++) { int ma = 0; for (int x = i - 4 * Dx[u], y = j - 4 * Dy[u], k = 0; k < 5; k++, x += Dx[u], y += Dy[u]) { int X = x, Y = y, can = 1; int NUM = 0, Max = 0, Min = 1e9; for (int l = 0; l < 5; l++) { if (X < 1 || X > len || Y < 1 || Y > len || c[X][Y] != per && c[X][Y]) can = 0; if (c[X][Y] == per || X == i && Y == j) NUM++, Max = max(Max, l), Min = min(Min, l); X += Dx[u], Y += Dy[u]; } if (!can) continue; int is = 1; if (X < 1 || X > len || Y < 1 || Y > len) is = 0; if (x - Dx[u] < 1 || x - Dx[u] > len || y - Dy[u] < 1 || y - Dy[u] > len) is = 0; if (is) is = !(c[X][Y]) + !(c[x - Dx[u]][y - Dy[u]]); ma = max(ma, ad[NUM][max(0, Max - Min + 1)][is]); } s[i][j] += int(ma * p * Rand()); } } } void P(string s) { for (int i = 0; i < s.size(); i++) { putchar(s[i]); Sleep(N); } putchar('\n'); } void chess(int i, int j) { if (c[i][j] == 0 && nowc == 7) __builtin_printf("十"); else if (c[i][j] == 0) __builtin_printf("V"); else if (c[i][j] == 2) __builtin_printf("●"); else __builtin_printf("○"); } void Output() { for (int i = 0; i <= len + 1; i++) { for (int j = 0; j <= len + 1; j++) { if (i == 0 || j == 0 || i == len + 1 || j == len + 1) __builtin_printf("■"); else chess(i, j); } putchar('\n'); } } int Down(int p) { int x = LX, y = LY; // cout << '-'; while (1) { gotoxy(x, y); color(14); chess(x, y); color(7); char cc = getch(); while (cc != 'w' && cc != 'a' && cc != 's' && cc != 'd' && cc != 'e' && cc != 'q') cc = getch(); if (cc == 'w' && x != 1) { gotoxy(x, y); chess(x, y); x--; } if (cc == 'a' && y != 1) { gotoxy(x, y); chess(x, y); y--; } if (cc == 's' && x != len) { gotoxy(x, y); chess(x, y); x++; } if (cc == 'd' && y != len) { gotoxy(x, y); chess(x, y); y++; } if (cc == 'e' && !c[x][y]) { LX = x, LY = y; c[x][y] = p; gotoxy(x, y); chess(x, y); gotoxy(35, 0); N = 10; P(" "); break; } if (cc == 'q') return 1; } return 0; } bool check() { for (int i = 1; i <= len; i++) { for (int j = 1; j <= len; j++) { int x = i, y = j, can = 1; for (int u = 0; u < 4; u++) { x = i, y = j, can = 1; if (!c[x][y]) can = 0; for (int k = 0; k < 5; k++) { if (x < 1 || x > len || y < 1 || y > len || c[x][y] != c[i][j]) can = 0; x = x + Dx[u], y = y + Dy[u]; } if (can) { x = i, y = j, can = 1; color(14); for (int k = 0; k < 5; k++) { gotoxy(x, y); __builtin_printf("×"); x = x + Dx[u], y = y + Dy[u]; } color(7); return true; } } } } return false; } void best_put(int p) { int ma = 0, x = 0, y = 0; for (int i = 1; i <= len; i++) for (int j = 1; j <= len; j++) { if (s[i][j] > ma || s[i][j] == ma && abs(i - len / 2) + abs(j - len / 2) < abs(x - len / 2) + abs(y - len / 2)) ma = s[i][j], x = i, y = j; } c[x][y] = p; gotoxy(x, y); chess(x, y); } void PLAY_AI(double co) { system("cls"); memset(c, 0, sizeof c); N = 10; P("Which color do you want to choose?"); P("1.black"); P("2.white"); int per, now = 1, res; cin >> per; system("cls"); system("color F"); Output(); HIDE(); while (!check()) { if (now == per) { gotoxy(35, 0); N = 30; P("Your turn, please place it"); if (Down(per)) return; } else { memset(s, 0, sizeof s); PRIORITY(now, 1); PRIORITY(((now - 1) ^ 1) + 1, co); best_put(now); } if (now == 1) now = 2; else now = 1; } Sleep(2000); system("cls"); gotoxy(len / 2, len / 2); if (now != per) { N = 100; P("You win!"); return ; } else { N = 100; P("You lost..."); return; } } void RULE() { system("cls"); N = 5; P("You'd better play this game with full screen."); P("Before the begining,you can choose the color you want."); P("Black chess is the first move."); Sleep(1000); P("The game rules are the same as Gobang."); Sleep(100); P("When in your turn, you can press the 'w' 'a' 's' 'd' keys to move your position."); P("If you determine the position of playing chess."); P("you can press the 'e' key to fall the chess."); Sleep(500); P("(PRESS ANY KEY TO LEAVE)"); getch(); system("cls"); } int main() { nowc = 7; HWND hwnd = GetForegroundWindow(); ShowWindow(hwnd, SW_MAXIMIZE); SetWindowPos(hwnd, NULL, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), SWP_SHOWWINDOW); HIDE(); init(); while (1) { N = 30; system("cls"); system("color B"); P("Welcome to GOBANG GAME."); P("1.Play with the computer"); P("2.Game Rules"); cin >> C; HIDE(); if (C == 1) PLAY_AI(0.8); else if (C == 2) RULE(); } return 0; } ```