-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rulebook.cs
executable file
·112 lines (99 loc) · 4.9 KB
/
Rulebook.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Chess
{
public class Rulebook
{
private List<Tuple<int, int>> validDestinations = new List<Tuple<int, int>>();
private Piece copyOfCurrentPiece;
private Gameboard gameboard;
private GameComponents info = new GameComponents();
public List<Tuple<int, int>> getValidMoves(Piece currentPiece, Gameboard gameboard, bool doRecurse = true)
{
this.copyOfCurrentPiece = new Piece(currentPiece);
this.gameboard = new Gameboard(gameboard);
switch (currentPiece.type)
{
case (int)type.rock: // tower
Rock rock = new Rock();
this.validDestinations = rock.Rules(currentPiece, gameboard);
if (doRecurse)
cleanUp();
return validDestinations;
case (int)type.knight: // horsie
Knight knight = new Knight();
this.validDestinations = knight.Rules(currentPiece, gameboard);
if (doRecurse)
cleanUp();
return validDestinations;
case (int)type.bishop: // springare
Bishop bishop = new Bishop();
this.validDestinations = bishop.Rules(currentPiece, gameboard);
if (doRecurse)
cleanUp();
return validDestinations;
case (int)type.queen: //los quuenos
Queen queen = new Queen();
this.validDestinations = queen.Rules(currentPiece, gameboard);
if (doRecurse)
cleanUp();
return validDestinations;
case (int)type.king: // los kingos
King king = new King();
this.validDestinations = king.Rules(currentPiece, gameboard);
if (doRecurse)
cleanUp();
return validDestinations;
case (int)type.pawn: // los farmeros
Pawn pawn = new Pawn();
this.validDestinations = pawn.Rules(currentPiece, gameboard);
if (doRecurse)
cleanUp();
return validDestinations;
}
return new List<Tuple<int,int>>();
}
private void cleanUp()
{
Gameboard copyOfGameboard = new Gameboard(gameboard);
Rulebook rulebook = new Rulebook();
List<Tuple<int, int>> copyOfValids = new List<Tuple<int, int>>();
bool foundIllegalMove = false;
Piece originalCurrentPiece;
int currentTeam = copyOfCurrentPiece.team;
int opponentTeam = info.getOpponent(currentTeam);
foreach (Tuple<int, int> validMove in validDestinations) // För varje möjligt drag för markerad Piece
{
copyOfGameboard = new Gameboard(gameboard);
originalCurrentPiece = copyOfGameboard.getPiece(copyOfCurrentPiece.row, copyOfCurrentPiece.column);
Piece destination = copyOfGameboard.getPiece(validMove.Item1, validMove.Item2);
copyOfGameboard.Move(originalCurrentPiece, destination); // Gör temporärt move
foundIllegalMove = false;
foreach (Piece opponent in copyOfGameboard.getTeam(opponentTeam)) // För varje Piece i team OPPONENT
{
List<Tuple<int,int>> validDest = rulebook.getValidMoves(opponent, copyOfGameboard, false); // Hämta valid moves för Piece i OPPONENT (Utan hänsyn till möjliga drag som sätter kungen i schack..)
foreach (Tuple<int,int> coordinate in validDest) // För varje möjligt drag för Piece i OPPONENT
{
if (copyOfGameboard.getPiece(coordinate.Item1, coordinate.Item2).type == (int)type.king) // Om Piece i OPPONENT kan ta kungen i CURRENT
{
foundIllegalMove = true;
break;
}
if (foundIllegalMove)
break;
}
if (foundIllegalMove)
break;
}
if (!foundIllegalMove)
copyOfValids.Add(new Tuple<int, int>(validMove.Item1, validMove.Item2));
copyOfGameboard.setPiece(originalCurrentPiece);
copyOfGameboard.setPiece(destination);
}
validDestinations = copyOfValids;
}
}
}