File size: 6,317 Bytes
7f3d66e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
171
172
173
174
175
176
177
178
179
// Chess Game for Retro Arcade
class ChessGame {
    constructor(canvas, ctx) {
        this.canvas = canvas;
        this.ctx = ctx;
        this.boardSize = Math.min(canvas.width, canvas.height) - 40;
        this.squareSize = this.boardSize / 8;
        this.board = this.initializeBoard();
        this.selectedPiece = null;
        this.currentPlayer = 'white';
        this.gameOver = false;
        this.moveHistory = [];
    }

    initializeBoard() {
        const board = [];
        for (let row = 0; row < 8; row++) {
            board[row] = [];
            for (let col = 0; col < 8; col++) {
                board[row][col] = this.getInitialPiece(row, col);
            }
        }
        return board;
    }

    getInitialPiece(row, col) {
        if (row === 1) return { type: 'pawn', color: 'black' };
        if (row === 6) return { type: 'pawn', color: 'white' };
        
        if (row === 0 || row === 7) {
            const color = row === 0 ? 'black' : 'white';
            const pieces = ['rook', 'knight', 'bishop', 'queen', 'king', 'bishop', 'knight', 'rook'];
            return { type: pieces[col], color: color };
        }
        
        return null;
    }

    draw() {
        this.ctx.fillStyle = '#8B4513';
        this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
        
        // Draw board
        for (let row = 0; row < 8; row++) {
            for (let col = 0; col < 8; col++) {
                const x = col * this.squareSize + 20;
                const y = row * this.squareSize + 20;
                
                // Square color
                this.ctx.fillStyle = (row + col) % 2 === 0 ? '#D2B48C' : '#8B4513';
                this.ctx.fillRect(x, y, this.squareSize, this.squareSize);
                
                // Highlight selected piece
                if (this.selectedPiece && this.selectedPiece.row === row && this.selectedPiece.col === col) {
                    this.ctx.strokeStyle = '#FFD700';
                    this.ctx.lineWidth = 3;
                    this.ctx.strokeRect(x, y, this.squareSize, this.squareSize);
                }
                
                // Draw piece
                const piece = this.board[row][col];
                if (piece) {
                    this.drawPiece(piece, x, y);
                }
            }
        }
        
        // Draw game info
        this.ctx.fillStyle = '#FFF';
        this.ctx.font = '16px Arial';
        this.ctx.fillText(`Current: ${this.currentPlayer}`, 20, this.canvas.height - 10);
        
        if (this.gameOver) {
            this.ctx.fillStyle = 'rgba(0,0,0,0.7)';
            this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
            this.ctx.fillStyle = '#FFD700';
            this.ctx.font = '30px Arial';
            this.ctx.textAlign = 'center';
            this.ctx.fillText('Game Over!', this.canvas.width/2, this.canvas.height/2);
        }
    }

    drawPiece(piece, x, y) {
        this.ctx.font = `${this.squareSize * 0.7}px Arial`;
        this.ctx.textAlign = 'center';
        this.ctx.textBaseline = 'middle';
        
        const pieces = {
            'king': '♔', 'queen': '♕', 'rook': '♖',
            'bishop': '♗', 'knight': '♘', 'pawn': '♙'
        };
        
        this.ctx.fillStyle = piece.color === 'white' ? '#FFF' : '#000';
        this.ctx.fillText(
            pieces[piece.type],
            x + this.squareSize/2,
            y + this.squareSize/2
        );
    }

    handleClick(x, y) {
        if (this.gameOver) return;
        
        const col = Math.floor((x - 20) / this.squareSize);
        const row = Math.floor((y - 20) / this.squareSize);
        
        if (col < 0 || col > 7 || row < 0 || row > 7) return;
        
        const piece = this.board[row][col];
        
        if (this.selectedPiece) {
            // Try to move
            if (this.isValidMove(this.selectedPiece, row, col)) {
                this.movePiece(this.selectedPiece, row, col);
                this.currentPlayer = this.currentPlayer === 'white' ? 'black' : 'white';
            }
            this.selectedPiece = null;
        } else if (piece && piece.color === this.currentPlayer) {
            this.selectedPiece = { row, col, piece };
        }
    }

    isValidMove(from, toRow, toCol) {
        const piece = from.piece;
        const rowDiff = Math.abs(toRow - from.row);
        const colDiff = Math.abs(toCol - from.col);
        
        // Basic validation (simplified chess rules)
        if (toRow < 0 || toRow > 7 || toCol < 0 || toCol > 7) return false;
        
        const targetPiece = this.board[toRow][toCol];
        if (targetPiece && targetPiece.color === piece.color) return false;
        
        switch(piece.type) {
            case 'pawn':
                const direction = piece.color === 'white' ? -1 : 1;
                if (colDiff === 0 && !targetPiece) {
                    if (toRow === from.row + direction) return true;
                    if ((from.row === 6 || from.row === 1) && toRow === from.row + 2 * direction) return true;
                }
                if (colDiff === 1 && toRow === from.row + direction && targetPiece) return true;
                return false;
                
            case 'rook':
                return (rowDiff === 0 || colDiff === 0);
                
            case 'bishop':
                return rowDiff === colDiff;
                
            case 'queen':
                return (rowDiff === 0 || colDiff === 0 || rowDiff === colDiff);
                
            case 'king':
                return rowDiff <= 1 && colDiff <= 1;
                
            case 'knight':
                return (rowDiff === 2 && colDiff === 1) || (rowDiff === 1 && colDiff === 2);
                
            default:
                return false;
        }
    }

    movePiece(from, toRow, toCol) {
        this.board[toRow][toCol] = from.piece;
        this.board[from.row][from.col] = null;
        this.moveHistory.push({
            from: { row: from.row, col: from.col },
            to: { row: toRow, col: toCol },
            piece: from.piece
        });
    }
}

// Export for use in main games.js
if (typeof module !== 'undefined' && module.exports) {
    module.exports = ChessGame;
}