#include <stdio.h> #include <iostream> #include <string.h> #include <stdlib.h> #include <conio.h> #include <io.h> #define HIGHT 20 #define WIGHT 80 #define N 8 #define BLACK 1 #define WHITE 2 #define BPOS 3 #define WPOS 4 #define SPACE 0 #define LEFT 75 #define UP 72 #define RIGHT 77 #define DOWN 80 #define ESC 27 using namespace std;
struct nod { int x, y; }point;
int black, white, first, continued, esc; int move[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};//up left down right int map[N][N], tp[N][N]; // 0->NULL 1->black 2->white char mode[16][3] = {"╔","═","╦","╗","╠","╬","╣","╚", "╩","╝","║", " ", "●", "○", "◆", "◇"};
void logo() { system("cls"); puts("\t _O_ 黑 _____ 白 _<>_ 棋 ___ "); puts("\t / \\ | | / \\ / _ \\"); puts("\t |==/=\\==| |[/_\\]| |==\\==/==| | / \\ |"); puts("\t | O O | / O O \\ | >< | | |\"| |"); puts("\t \\ V / /\\ - /\\ ,-\\ () /-. \\ X /"); puts("\t "); puts("\t /`---'\\ /`---'\\ V( `-====-' )V /`---'\\"); puts("\t O'_:_`O O'M|M`O (_____:|_____) O'_|_`O "); puts("\t -- -- -- -- ---- ---- -- -- "); puts("\t 请选择先手 \n\n"); puts("\t\t 1、 ● \n"); puts("\t\t 2、 ○ \n"); puts("\t\t 3、 退出 \n"); printf("\t\t 您的选择:"); scanf("%d", &first); if(first != 1 && first != 2 && first != 3) logo(); } void draw(char head[], char body[], char cross[], char tail[], int size, int row, int mp[N][N]) { printf("%s", head); for(int i = 1; i < 2 * size; i ++) { if(i % 2 == 0) { printf("%s", cross); } else { int t = (i + 1) / 2 - 1, r = row / 2 - 1; if(row % 2 == 0 && ((i + 1) % 2 == 0)) { printf("%s", mode[11 + mp[r][t]]); } else { printf("%s", body); } } } printf("%s\n", tail); }
void set_map(int size, int mp[N][N]) { int temp; system("cls"); temp = (HIGHT - size) / 2; puts("\n\t\t\t\t黑白棋"); puts("\t\t\t 用ENTER放置棋子 用ESC键退出\n"); for(int i = 1; i <= 2 * size + 1; i ++) { temp = (WIGHT - 2 * size + 1) / 2 - 2 * size + 1; for(int j = 0; j < temp; j ++) putchar(' '); if(i == 1) draw(mode[0], mode[1], mode[2], mode[3], size, i, mp); else if(i == 2 * size + 1) draw(mode[7], mode[1], mode[8], mode[9], size, i, mp); else if(i % 2) draw(mode[4], mode[1], mode[5], mode[6], size, i, mp); else draw(mode[10], mode[11], mode[10], mode[10], size, i, mp); } printf("\n\t记分牌: ○:%d \t ●:%d\n", white, black); }
void init() { memset(map, 0, sizeof(map)); map[3][3] = map[4][4] = BLACK; map[4][3] = map[3][4] = WHITE; //map[0][0] = WHITE; //map[1][0] = BLACK; //map[0][1] = BLACK; point.x = point.y = 0; black = 2; white = 2; set_map(8, map); }
int move8[8][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
bool inmap(int x, int y) { if(x < 0 || y < 0 || x >= N || y >= N) return false; return true; }
bool judge(int x, int y, int col, int mp[N][N]) { if(mp[x][y] == WHITE || mp[x][y] == BLACK) return false; for(int dic = 0; dic < 8; dic ++) { int tx = x + move8[dic][0], ty = y + move8[dic][1], flag = 0; while(mp[tx][ty] == 3 - col && inmap(tx, ty)) { tx += move8[dic][0]; ty += move8[dic][1]; flag = 1; } if(!flag) continue; if( inmap(tx, ty) ) { if(mp[tx][ty] == col) return true; } } return false; }
void change(int x, int y, int col, int mp[N][N]) { for(int dic = 0; dic < 8; dic ++) { int tx = x + move8[dic][0], ty = y + move8[dic][1], flag = 0; while(mp[tx][ty] == 3 - col && inmap(tx, ty)) { tx += move8[dic][0]; ty += move8[dic][1]; flag = 1; } if(!flag) continue; if( inmap(tx, ty) ) { if(mp[tx][ty] == col) { int xx = x, yy = y; while(xx != tx || yy != ty) { mp[xx][yy] = col; xx += move8[dic][0]; yy += move8[dic][1]; } } } } }
void calc(int mp[N][N]) { white = black = 0; for(int i = 0; i < N; i ++) for(int j = 0; j < N; j ++) { if(mp[i][j] == WHITE) white ++; if(mp[i][j] == BLACK) black ++; } }
bool isok(int col, int mp[N][N]) { for(int i = 0; i < N; i ++) { for(int j = 0; j < N; j ++) { if( judge(i, j, col, mp) ) return true; } } return false; } void GameOver() { system("cls"); cout << "\t\t\t\t黑白棋\n\n" << endl; cout << "\t\t※※※※※※※※※※※※※※※※※※※※※" << endl; cout << "\t\t※ ※" << endl; cout << "\t\t※ ※" << endl; cout << "\t\t※ ※" << endl; cout << "\t\t※ ※" << endl; cout << "\t\t※"; cout <<" GAME OVER "; cout << "※" << endl; cout << "\t\t※ ※" << endl; cout << "\t\t※ "; printf( "●:%2d ○:%2d ※\n",white, black); ; cout << "\t\t※ ※" << endl; cout << "\t\t※ ※" << endl; if( black < white) { cout << "\t\t※ ● wins! ※" << endl; } else if( black > white) { cout << "\t\t※ ○ wins! ※" << endl; } else { cout << "\t\t※ TIE ※" << endl; } cout << "\t\t※ ※" << endl; cout << "\t\t※ ※" << endl; cout << "\t\t※ ※" << endl; cout << "\t\t※ ※" << endl; cout << "\t\t※※※※※※※※※※※※※※※※※※※※※" << endl; cout << endl << endl; } void final() { GameOver(); printf( "\t\t 是否继续(Y/N): "); char ss[2]; scanf("%s", ss); if(ss[0] == 'Y') { system("pause"); return; } else { esc = 1; return; } }
void play() { int player = 2 + first; int last = map[0][0]; while(true) { int flag = 0; map[point.x][point.y] = last; set_map(8, map); if( !isok(player - 2, map) && !isok(5 - player, map) ) { final(); return; } else if( !isok(player - 2, map) ) { flag = 1; player = 7 - player; } map[0][0] = player; set_map(8, map); if(flag) { printf("本方无棋可走,更换棋手\n"); } char keyborad; while( (keyborad = getch()) != '\x0d') { keyborad = getch(); if(keyborad == ESC) { esc = 1; return; } int dic; switch(keyborad) { case UP: dic = 0; break; case LEFT: dic = 1; break; case DOWN: dic = 2; break; case RIGHT: dic = 3; break; default: keyborad = getch(); continue; } int tx = (point.x + N + move[dic][0]) % N, ty = (point.y + N + move[dic][1]) % N; map[point.x][point.y] = last; point.x = tx; point.y = ty; last = map[point.x][point.y]; map[point.x][point.y] = player; set_map(8, map); } if( judge(point.x, point.y, player - 2, map) && last == SPACE) { map[point.x][point.y] = player - 2; change(point.x, point.y, player - 2, map); calc(map); player = 3 + WPOS - player; point.x = point.y = 0; last = map[0][0]; map[0][0] = player; set_map(8, map); } else { set_map(8, map); puts("\t \t \t 该位置不能放置"); system("pause"); map[point.x][point.y] = last; last = map[0][0]; point.x = point.y = 0; } } }
int main() { logo(); if(first == 3) return 0; continued = 1; esc = 0; while(continued && !esc) { init(); play(); } }
|