-
Notifications
You must be signed in to change notification settings - Fork 22
/
ish.go.view.text.js
164 lines (137 loc) · 4.07 KB
/
ish.go.view.text.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Ish.Go namespace declaration
var Ish = Ish || {};
Ish.Go = Ish.Go || {};
// begin Ish.Go.View namespace
Ish.Go.View = new function() {
var isBoardMarked = false;
/**
* Prints scores on web page
*/
this.printScores = function(aId) {
var id = aId || "score";
Ish.Go.Logic.setScores();
var p1 = gGameState.player1;
var p2 = gGameState.player2;
$("#" + id).html("Score:" +
"<br> " +
p1.color + ": " + p1.score +
"<br> " +
p2.color + ": " + p2.score);
};
/**
* Prints color of current turn on web page
*/
this.printTurn = function(aId) {
var id = aId || 'turn';
$("#" + id).text("Current turn: " + gGameState.currentPlayer.toString());
};
/**
* Prints board on web page
*/
this.printBoard = function(aId) {
var id = aId || 'board';
var sBoard = "";
for (var y = 0; y < gGameState.boardHeight; y++) {
for (var x = 0; x < gGameState.boardWidth; x++) {
var point = new Point(y, x);
sBoard += "<span class='point' onclick='Ish.Go.Logic.move(" + y + "," + x + ");Ish.Go.View.redraw()'>";
sBoard += " " + gGameState.getPointStateAt(point) + " ";
sBoard += "</span>";
}
sBoard += "<br>";
}
$("#" + id).html(sBoard);
};
/**
* Prints board marked with territies on web page
*/
this.printMarkedBoard = function(aId) {
var id = aId || 'board';
var markedBoard = Ish.Go.Logic.getMarkedBoard();
var sBoard = "";
for (var y = 0; y < gGameState.boardHeight; y++) {
for (var x = 0; x < gGameState.boardWidth; x++) {
sBoard += "<span class='point marked'>";
sBoard += " " + markedBoard[y][x] + " ";
sBoard += "</span>";
}
sBoard += "<br>";
}
$("#" + id).html(sBoard);
};
/**
* Prints code defining current game state on web page
*/
this.printGameState = function(aId) {
var id = aId || 'gameState';
var sBoard = "";
// Initialize game state
sBoard += "gGameState = new GameState(\n";
sBoard += "\t" + gGameState.boardWidth + ",\n";
sBoard += "\t" + gGameState.boardHeight + ",\n";
sBoard += "\tnew Player(Constants.Color.BLACK, Constants.PointState.BLACK),\n";
sBoard += "\tnew Player(Constants.Color.WHITE, Constants.PointState.WHITE)\n";
sBoard += ");\n";
// Set current player
sBoard += "gGameState.currentPlayer = " +
(gGameState.currentPlayer == gGameState.player1 ?
"gGameState.player1;\n" :
"gGameState.player2;\n");
// Set board
for (var y = 0; y < gGameState.boardHeight; y++) {
sBoard += "gGameState.board[" + y + "] = [";
for (var x = 0; x < gGameState.boardWidth; x++) {
sBoard += "\"" + gGameState.board[y][x] + "\",";
}
sBoard = sBoard.substring(0, sBoard.length-1);
sBoard += "];\n";
}
// Set previous board
for (var y = 0; y < gGameState.boardHeight; y++) {
sBoard += "gGameState.previousBoard[" + y + "] = [";
for (var x = 0; x < gGameState.boardWidth; x++) {
sBoard += "\"" + gGameState.previousBoard[y][x] + "\",";
}
sBoard = sBoard.substring(0, sBoard.length-1);
sBoard += "];\n";
}
$("#" + id).html("<textarea>" + sBoard + "</textarea>");
};
/**
* Toggles between showing a regular or marked board.
* Merely calls appropriate print functions.
*/
this.toggleMarkedBoard = function() {
isBoardMarked ? this.printBoard() : this.printMarkedBoard();
isBoardMarked = !isBoardMarked;
};
/**
* Prompts the user for a board size, and starts a new game.
*/
this.startNewGame = function() {
var size = prompt("Please provide a board size:", "9x9").split("x");
var width = parseInt(size[0]);
var height = parseInt(size[1]);
if ( isNaN(width) || isNaN(height) ) {
alert("Invalid board size.");
return;
}
Ish.Go.Logic.newGame(width, height);
this.redraw();
};
this.redraw = function() {
this.printBoard();
this.printTurn();
this.printScores();
};
this.init = function() {
// Initialize game state
gGameState = new GameState(
9,
9,
new Player(Constants.Color.BLACK, Constants.PointState.BLACK),
new Player(Constants.Color.WHITE, Constants.PointState.WHITE)
);
this.redraw();
};
}; // end Ish.Go.View namespace