Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <conio.h>
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <time.h>
- #include <windows.h>
- using namespace std;
- char k;
- int p=0;
- vector<string> map(9, "");
- string chess;
- bool IsMoveLeft(){ //判定還有沒有格子可以下
- for(int i = 0 ; i < 9 ; i++){
- if( map[i] == "" ){
- return true;
- }
- }
- return false;
- }
- //XO
- int evalboard(){ //算分
- for( int i = 0 ; i <=6 ; i+=3 ){
- if( map[i] != "" && map[i] == map[i+1] && map[i] == map[i+2] ){
- if( map[i] == "O"){
- return 10;
- }
- else{
- return -10;
- }
- }
- }
- for( int i = 0 ; i <=2 ; i++ ){
- if( map[i] != "" && map[i] == map[i+3] && map[i] == map[i+6] ){
- if( map[i] == "O"){
- return 10;
- }
- else{
- return -10;
- }
- }
- }
- if( map[0] != "" && map[0] == map[4] && map[0] == map[8] ){
- if( map[0] == "O"){
- return 10;
- }
- else{
- return -10;
- }
- }
- if( map[2] != "" && map[2] == map[4] && map[2] == map[6] ){
- if( map[2] == "O"){
- return 10;
- }
- else{
- return -10;
- }
- }
- return 0;
- }
- void draw() {
- system("cls"); //cls means clear screen
- cout << IsMoveLeft() <<endl;
- cout << evalboard() <<endl;
- for( int i = 0 ; i <9 ; i++ ){
- if(map[i] != ""){
- cout << map[i];
- }
- else if(i ==p){
- cout << "□";
- }
- else{
- cout << "■";
- }
- if( i % 3 == 2 ){
- cout << endl;
- }
- }
- }
- int minimax( int depth , bool ismax ){ //MiniMax決策樹
- int score = evalboard();
- if( score != 0 ){
- return score;
- }
- if( IsMoveLeft() == false ){
- return 0;
- }
- int best , a;
- if(ismax){
- best = -11;
- for(int i = 0 ; i < 9 ; i++){
- if(map[i] == ""){
- map[i] = "O";
- a = minimax(depth+1 , not ismax) - depth;
- if( a > best ){
- best = a;
- }
- map[i] = "";
- }
- }
- }
- else{
- best = 11;
- for(int i = 0 ; i < 9 ; i++){
- if( map[i] == "" ){
- map[i] = "X";
- a = minimax(depth+1 , not ismax ) + depth;
- if( a < best ){
- best = a;
- }
- map[i] = "";
- }
- }
- }
- return best;
- }
- void BestMove(){
- if( IsMoveLeft() == false ){
- draw();
- cout << "tie\n";
- }
- else{
- int bestval = -11;
- int bestidx = -1;
- for(int i = 0 ; i < 9 ; i++){
- if(map[i] == ""){
- map[i] = "O";
- int moveval = minimax(0 , false);
- map[i] = "";
- if( moveval > bestval ){
- bestval = moveval;
- bestidx = i;
- }
- }
- }
- if( bestidx > -1 ){
- map[bestidx] = "O";
- }
- }
- }
- int main() {
- chess="X"; //XO全形字元,不然棋盤會歪掉,shift+space可以切換全形半形
- draw();
- string whowin="";
- while (1) {
- if (kbhit()) {
- k=_getch();
- if( k == 'q' ){
- break;
- }
- else if( k == 'e' ){
- p = p-3;
- }
- else if( k == 'd' ){
- p = p+3;
- }
- else if( k == 's' ){
- p = p-1;
- }
- else if( k == 'f' ){
- p = p+1;
- }
- else if( k == 'c' ){
- if( map[p] == "" ){
- map[p] = chess;
- if( chess == "X" ){
- BestMove();
- //chess = "O";
- }
- else{
- chess = "X";
- }
- }
- }
- for( int i = 0 ; i <=6 ; i+=3 ){
- if( map[i] != "" && map[i] == map[i+1] && map[i] == map[i+2] ){
- whowin = map[i];
- }
- }
- for( int i = 0 ; i <=2 ; i++ ){
- if( map[i] != "" && map[i] == map[i+3] && map[i] == map[i+6] ){
- whowin = map[i];
- }
- }
- if( map[0] != "" && map[0] == map[4] && map[0] == map[8] ){
- whowin = map[0];
- }
- if( map[2] != "" && map[2] == map[4] && map[2] == map[6] ){
- whowin = map[2];
- }
- draw();
- if( whowin != "" ){
- cout << whowin << "win";
- break;
- }
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment