-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
98 lines (90 loc) · 3.61 KB
/
Program.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConnectFour
{
class Program
{
static void Main(string[] args)
{
int tries = 0;
int humanV = 0;
int botV = 0;
int draws = 0;
int cols = 7;
int rows = 6;
int depth = 6;
while (tries < 5)
{
Console.Clear();
State board = new State(rows, cols, 1);
AiAlphaBeta bot = new AiAlphaBeta();
int botTurn = (tries % 2 == 0) ? 2 : 1;
int playerTurn = botTurn == 1 ? 2 : 1;
bot.StartGame(botTurn, rows, cols, depth);
while (board.GameIsFinished() == -1)
{
Console.Clear();
if(botTurn == 1)
{
Console.WriteLine("Computer Piece | X |");
Console.WriteLine("Player Piece | O |");
ComputerMove(bot, botTurn, board);
PlayerMove(bot, playerTurn, board);
}
else
{
Console.WriteLine("Player Piece | X |");
Console.WriteLine("Computer Piece | O |");
PlayerMove(bot, playerTurn, board);
ComputerMove(bot, botTurn, board);
}
Console.WriteLine();
}
if (board.GetWinner() == playerTurn) humanV++;
else if (board.GetWinner() == botTurn) botV++;
else draws++;
tries++;
Console.WriteLine(board.PrintBoard());
Console.WriteLine("GAME " + tries + " CONCLUDED!");
Console.WriteLine($"Human ({humanV}) VS Bot ({botV}), Draws: {draws}");
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
Console.Clear();
}
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
private static void ComputerMove(AiAlphaBeta ai, int playerID, State board)
{
int statesSeen = State.STATES_CREATED;
DateTime calcStart = DateTime.Now;
int play1 = ai.CalculateNextMove();
ai.InsertCoin(play1, playerID);
board.InsertCoin(play1, playerID);
Console.WriteLine($"COMPUTER MOVE: C{play1 + playerID}");
Console.WriteLine($"States looked at: {State.STATES_CREATED - statesSeen}");
Console.WriteLine($"Time taken: {DateTime.Now.Subtract(calcStart).TotalSeconds} s");
Console.WriteLine(board.PrintBoard());
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
private static void PlayerMove(AiAlphaBeta ai, int playerID, State board)
{
Console.WriteLine(board.PrintBoard());
Console.Write($"PLAYER MOVE C({1} - {board.GetCols()}): ");
int move = -1;
bool isInt = true;
do
{
if (isInt == false) Console.WriteLine("Invalid Col!");
isInt = int.TryParse(Console.ReadLine(), out move);
} while ((move < 0 || move > board.GetCols()) || !isInt );
ai.InsertCoin(move-1, playerID);
board.InsertCoin(move-1, playerID);
Console.Clear();
}
}
}