-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlayBoard.js
122 lines (114 loc) · 2.79 KB
/
PlayBoard.js
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
113
114
115
116
117
118
119
120
121
122
/**********************************************************
Data initialization
*********************************************************/
/*
initializes the board before users start placing their pieces
*/
function setupBoard(){
var i;
var j;
matrixSize = 10;
board = new Array(matrixSize);
for (i=0; i < matrixSize; i++)
{
board[i] = new Array(matrixSize);
for (j=0; j < matrixSize; j++)
{
board[i][j] = 0;
}
}
};
/********************************************************************
Utility functions
**********************************************************************/
/*
The board is 10x10, and has 2 regions that are off limits.
This function determines if a given location is a valid place to move a unit
*/
function isValidSpot(x, y){
//Verify that the values lie within the board
if ( (x<0)||(x>9)||(y<0)||(y>9))
{
return 0;
}
//The following condition checks for invalid location.
if ( ( (x==2)||(x==3)||(x==6)||(x==7) ) && ( (y==4)||(y==5) ) )
{
return 0;
}
//Otherwise, this location is a valid location on the map.
return 1;
};
/*
input: a unit from the board
output: the number of a unit in the player list
*/
function getBoardUnit(unitVal){
if (unitVal > 0) return unitVal-1;
return -1-unitVal;
};
/*
input: a unit from the board
output: the unit related to that value
*/
function getUnitInfo(unitVal){
//if (isSinglePlay){
if (unitVal > 0) return Units[0][unitVal-1];
return Units[1][-1-unitVal];
//}
//else
/*if (unitVal > 0){
if(isPlayer1) return myUnits[unitVal-1];
return Units[1][unitVal-1];
}
//else
if(isPlayer1) return myUnits[-1-unitVal];
return Units[1][-1-unitVal];*/
};
/*
Returns 0 if units are equal,
-2 if unit1 captured the flag,
2 if unit2 captured the flag,
-1 if unit1 destroyed unit2
1 if unit2 destroyed unit1
*/
function decideWinner(unitAttack, unitDefense)
{
var unit1Rank = getSoldierRank(unitAttack);
var unit2Rank = getSoldierRank(unitDefense);
//End Case: The flag was captured!
if (unit1Rank==11) return 2;
if (unit2Rank==11) return -2;
//Case 1: units are the same. Both die
if (unit1Rank == unit2Rank)
{
return 0;
}
//Case 2: Special case involving a spy
if (unit1Rank==0)
{
if (unit2Rank==1) return -1;
return 1;
}
if (unit2Rank==0)
{
//if (unit1Rank==1) return 1;
return -1; //when a spy is attacked, it loses no matter what
}
//Case 4: Special case involving a mine
if (unit1Rank == 10)
{
//winner decided based on whether opponent is a miner (8)
if (unit2Rank == 8) return 1;
return -1;
}
if (unit2Rank == 10 )
{
//winner decided based on whether opponent is a miner (8)
if (unit1Rank == 8) return -1;
return 1;
}
//Case 6: Remaining cases, outcome determined by rank.
if (unit1Rank < unit2Rank) return -1;
return 1;
}