-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
170 lines (159 loc) · 5.98 KB
/
game.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
165
166
167
168
169
170
import {loadPgnAndReflect} from './pgn.js';
import {NO_CELL_I} from './constants.js';
import {createBoard} from './board.js';
import {createMenu} from './menu.js';
/**
* Keep object flat (!)
*
* @typedef {Object} Game
* @property {boolean} turnWhite (Otherwise it's black, of course)
* @property {boolean} castleKingsideWhite
* @property {boolean} castleQueensideWhite
* @property {boolean} castleKingsideBlack
* @property {boolean} castleQueensideBlack
* @property {BigInt} enpassant (The cell i containing a pawn that made a two cell advance in the previous turn, or NO_CELL_I. Shared by both sides since it's only relevant for the immediate next move of the opponent.)
* @property {number} fiftyTurnCounter Half-turns (a full turn is a move by both players) since the last capture or pawn-move. At 50 the game ends in a draw. Offset 0. Increments after each player.
* @property {number} fullTurnCounter Full-turns (the current nth move for each player). Offset 1. Increments after each black move.
* @property {'' | 'queen' | 'rook' | 'knight' | 'bishop'} promotionDefault
* @property {BigInt} [prevFrom]
* @property {BigInt} [prevTo]
* @property {BigInt} white
* @property {BigInt} black
* @property {BigInt} rooks
* @property {BigInt} bishops
* @property {BigInt} knights
* @property {BigInt} queens
* @property {BigInt} kings
* @property {BigInt} pawns
* @property {Map<string, number>} threefold This one should only be updated when making a real move...
*/
/**
* The state data for the whole web page
*
* @typedef GlobalState {Object}
* @property {'none' | 'filled' | 'white' | 'black' | 'pawns' | 'kings' | 'queens' | 'knights' | 'bishops' | 'rooks' | 'can_move' | 'is_checked_white' | 'is_checked_black' } currentOverlay (Global default, local state can override this)
* @property {'none' | 'select' | 'move'} autoArrowClear Maybe we can allow individual boards to override this but why
*/
/**
* Pointer related state. I like mouse state better :shrug:
*
* @typedef MouseState {Object}
* @property {boolean} pointerDownStatus
* @property {number} pointerDownX
* @property {number} pointerDownY
* @property {boolean} deselectCell Used to determine whether the cell should be deselected on pointerup
* @property {BigInt} pointerDownCellI
* @property {'lmb' | 'mmb' | 'rmb'} pointerDownButton
* @property {BigInt} pointerOverCellI
* @property {boolean} pointerDragging
* @property {undefined|Element} currentDragIcon
* @property {Element} currentArrow
*/
/**
* The html state of a single board
*
* @typedef Lhtml {Object}
* @property {Element} root
* @property {Element} board
* @property {Element[]} cells First element of cells is h1, last element is a8, going 1-8 then a-h
* @property {Element} castleQueensideWhite
* @property {Element} castleKingsideWhite
* @property {Element} castleQueensideBlack
* @property {Element} castleKingsideBlack
* @property {Element} enpassant
* @property {Element} turns
* @property {Element} turn50
* @property {Element} three
* @property {Element} spanWhite
* @property {Element} spanBlack
* @property {Element} history
* @property {Element} pgnInput
* @property {Element} fenInput
* @property {Element} fenCurrent
* @property {Element} showAll
* @property {Element} turnWhite
* @property {Element} turnBlack
* @property {Element} thresh
*/
/**
* @typedef HistoryMove {Object}
* @property {number} turn
* @property {string} fen
* @property {boolean} white
* @property {'' | 'R' | 'N' | 'B' | 'Q' | 'K'} piece
* @property {string} from
* @property {BigInt} fromi
* @property {BigInt} fromn
* @property {string} to
* @property {BigInt} toi
* @property {BigInt} ton
* @property {string} an
* @property {Game} [beforeState]
*/
/**
* @typedef History {Object}
* @property {'1-0' | '0-1' | '1/2-1/2' | '*'} end
* @property {number} index
* @property {HistoryMove[]} moves The record of moves made (or imported pgn moves). Each step holds the G, its FEN, and meta data cache for visuals. Cleared when you load a FEN or PGN.
* @property {Game} [finalState] Only after calling preloadPgn with the includeBeforeGameState option
*
* TODO?:
* - alt-lines?
* - meta data
*/
/**
* The state data for one board and everything around it.
*
* @typedef LocalState {Object}
* @property {string} uid A unique string for this game. Used, at least, to namespace radio button names which are otherwise global.
* @property {Game} G The implicit game state for this board
* @property {History} [history] Only available
* @property {Lhtml} html
* @property {'' | 'none' | 'filled' | 'white' | 'black' | 'pawns' | 'kings' | 'queens' | 'knights' | 'bishops' | 'rooks' | 'can_move' | 'is_checked_white' | 'is_checked_black' } currentOverlay (Overrides the global value)
* @property {Map<string, Element>} arrowMap
* @property {BigInt} currentCell_i Current selection cell index
* @property {BigInt} currentCell_n Current selection cell bit
* @property {BigInt} currentTarget_i Only for debugging. Will ignore any other cell when doing attack checks etc.
* @property {'none' | 'strict'} validationMode
* @property {number} reflectTimer
* @property {number} autoPlayTimer
*/
/**
* @type {GlobalState}
*/
export const S = {
currentOverlay: 'can_move',
autoArrowClear: 'select',
};
/**
* (Global)
* @type {MouseState}
*/
export const M = {
pointerDownStatus: false,
pointerDownX: 0,
pointerDownY: 0,
pointerDownCellI: NO_CELL_I,
pointerDownButton: 'lmb',
pointerOverCellI: NO_CELL_I,
currentArrow: undefined,
pointerDragging: false,
currentDragIcon: undefined,
deselectCell: false,
};
{
// (This will later allow us to spawn multiple boards, but for now it's sort of global)
const L = createBoard('1');
createMenu(L);
loadPgnAndReflect(L, L.html.pgnInput.value);
}
// {
// /**
// // (This will later allow us to spawn multiple boards, but for now it's sort of global)
// const L = createBoard('2');
// createMenu(L);
// L.G = parseFen('r3k2r/8/8/8/8/8/8/R3K2R w KQkq - 0 1');
// // initPgn(L, L.html.pgnInput.value);
// reflect(L);
// }
//