|
1 | 1 | using System;
|
| 2 | +using System.Collections.Generic; |
2 | 3 | using System.Text;
|
3 | 4 | using KingsTableConsoleEdition.Interfaces;
|
4 | 5 | namespace KingsTableConsoleEdition
|
@@ -71,25 +72,79 @@ public bool MoveIsValid(int[][] move)
|
71 | 72 | public void ApplyMove(int[][] move)
|
72 | 73 | {
|
73 | 74 | Console.WriteLine("Applying Move");
|
| 75 | + //Console.Write(move[0][0]); |
| 76 | + //Console.WriteLine(move[0][1]); |
74 | 77 |
|
75 | 78 | //clear display characters
|
76 | 79 |
|
77 | 80 | //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] }; |
81 | 84 | board.SetPositionToValue(position, displayChar);
|
82 | 85 | }
|
83 | 86 | //try to move unit to second position, then clear display characters
|
84 | 87 | //if move fails then catch and do nothing
|
85 | 88 | }
|
86 | 89 |
|
87 |
| - public int[][] GetMovesForPieceAt(int[] position) |
| 90 | + public List<int[]> GetMovesForPieceAt(int[] position) |
88 | 91 | {
|
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; |
93 | 148 | }
|
94 | 149 |
|
95 | 150 |
|
|
0 commit comments