Skip to content

Commit a34aff8

Browse files
committed
implement finding all possible moves for a piece.
1 parent 0090ae7 commit a34aff8

File tree

3 files changed

+71
-9
lines changed

3 files changed

+71
-9
lines changed

KingsTableConsoleEdition/Board.cs

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public void SetBoard(char[,] newBoard){
2929
return currentBoard;
3030
}
3131

32+
public char GetValueAt(int[] position)
33+
{
34+
return currentBoard[position[0], position[1]];
35+
}
36+
3237
public void SetPositionToValue(int[] position, char value)
3338
{
3439
try

KingsTableConsoleEdition/Interfaces/IRules.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Collections.Generic;
3+
24
namespace KingsTableConsoleEdition.Interfaces
35
{
46
public interface IRules
@@ -8,6 +10,6 @@ public interface IRules
810
bool GameContinues();
911
bool MoveIsValid(int[][] move);
1012
void ApplyMove(int[][] move);
11-
int[][] GetMovesForPieceAt(int[] position);
13+
List<int[]> GetMovesForPieceAt(int[] position);
1214
}
1315
}

KingsTableConsoleEdition/MainRules.cs

+63-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Text;
34
using KingsTableConsoleEdition.Interfaces;
45
namespace KingsTableConsoleEdition
@@ -71,25 +72,79 @@ public bool MoveIsValid(int[][] move)
7172
public void ApplyMove(int[][] move)
7273
{
7374
Console.WriteLine("Applying Move");
75+
//Console.Write(move[0][0]);
76+
//Console.WriteLine(move[0][1]);
7477

7578
//clear display characters
7679

7780
//display moves for unit at first position
78-
int[][] moves = GetMovesForPieceAt(move[0]);
79-
for (int i = 0; i < moves.Length; i++){
80-
int[] position = new int[] { moves[i][0], moves[i][1] };
81+
List<int[]> moves = GetMovesForPieceAt(move[0]);
82+
for (int i = 0; i < moves.Count; i++){
83+
int[] position ={ moves[i][0], moves[i][1] };
8184
board.SetPositionToValue(position, displayChar);
8285
}
8386
//try to move unit to second position, then clear display characters
8487
//if move fails then catch and do nothing
8588
}
8689

87-
public int[][] GetMovesForPieceAt(int[] position)
90+
public List<int[]> GetMovesForPieceAt(int[] position)
8891
{
89-
int[] test = new int[] { 1, 1 };
90-
int[] test2 = new int[] { 1, 2 };
91-
int[][] toReturn = {test, test2};
92-
return toReturn;
92+
int y = position[0];
93+
int x = position[1];
94+
//Console.Write("y = " + y);
95+
//Console.WriteLine(", x = " + x);
96+
List<int[]> moves = new List<int[]>();
97+
98+
// look up
99+
for (int i = y - 1; i >= 0; i--){
100+
int[] tempPosition = { i, x };
101+
char occupant = board.GetValueAt(tempPosition);
102+
if(occupant == emptyChar){
103+
moves.Add(tempPosition);
104+
}else{
105+
break;
106+
}
107+
}
108+
// look right
109+
for (int i = x + 1; i <= 10; i++)
110+
{
111+
int[] tempPosition = { y, i };
112+
char occupant = board.GetValueAt(tempPosition);
113+
if (occupant == emptyChar)
114+
{
115+
moves.Add(tempPosition);
116+
}else{
117+
break;
118+
}
119+
}
120+
// look down
121+
for (int i = y + 1; i <= 10; i++)
122+
{
123+
int[] tempPosition = { i, x };
124+
char occupant = board.GetValueAt(tempPosition);
125+
if (occupant == emptyChar)
126+
{
127+
moves.Add(tempPosition);
128+
}else
129+
{
130+
break;
131+
}
132+
}
133+
// look left
134+
for (int i = x - 1; i >= 0; i--)
135+
{
136+
int[] tempPosition = { y, i };
137+
char occupant = board.GetValueAt(tempPosition);
138+
if (occupant == emptyChar)
139+
{
140+
moves.Add(tempPosition);
141+
}else
142+
{
143+
break;
144+
}
145+
}
146+
147+
return moves;
93148
}
94149

95150

0 commit comments

Comments
 (0)