got basic game working

This commit is contained in:
Shuozhe 2021-06-29 18:24:37 +02:00
parent ea6ef5b34a
commit 77146fdafd
2 changed files with 12 additions and 21 deletions

View File

@ -1,5 +1,3 @@
using ProjectGrid;
using ProjectGrid.Data;
using ProjectGrid.Models; using ProjectGrid.Models;
using System; using System;
@ -13,21 +11,27 @@ namespace ProjectGrid
public class TicTacTocManager : ITicTacTocManager public class TicTacTocManager : ITicTacTocManager
{ {
//private ITicTacToctRepository _repo; //private ITicTacToctRepository _repo;
private int _currentPlayer;
private TicTacTocBoard _board; private TicTacTocBoard _board;
public TicTacTocBoard Board => _board; public TicTacTocBoard Board => _board;
public TicTacTocManager(/*ITicTacToctRepository repo*/) public TicTacTocManager(/*ITicTacToctRepository repo*/)
{ {
//_repo = repo; //_repo = repo;
_currentPlayer = 1;
Restart(); Restart();
} }
public bool NextMove(TicTacTocRequest move) public bool NextMove(TicTacTocRequest move)
{ {
return _board.SetFieldValue(move.Player, move.Field); //move.PosX, move.PosY); var result = _board.SetFieldValue(_currentPlayer, move.Field); //move.PosX, move.PosY);
// switch between 1 and 2
_currentPlayer = 3 - _currentPlayer;
return result;
} }
public TicTacTocResponse GetResponse(int player = 0) public TicTacTocResponse GetResponse(int player = 0)
@ -39,11 +43,11 @@ namespace ProjectGrid
response.Board = _board.Field; response.Board = _board.Field;
return response; return response;
} }
public void Restart() public void Restart()
{ {
_currentPlayer = new Random().Next(1, 2);
_board = new TicTacTocBoard(); _board = new TicTacTocBoard();
} }
} }

View File

@ -9,7 +9,9 @@
</span> </span>
<br /> <br />
<span id="p1_score">Player One :{{this.game.players[0].score}}</span> <span id="p1_score">Player One :{{this.game.players[0].score}}</span>
<br />
<span id="p2_score">Player Two :{{this.game.players[1].score}}</span> <span id="p2_score">Player Two :{{this.game.players[1].score}}</span>
<br />
<span id="draw_score">Draw :{{this.game.draw_score}}</span> <span id="draw_score">Draw :{{this.game.draw_score}}</span>
</div> </div>
<button class="replay" id="restartBtn" @click="resetGame()">Restart</button> <button class="replay" id="restartBtn" @click="resetGame()">Restart</button>
@ -35,6 +37,7 @@
import { setPiece, restart } from '../services/TicTacTocService' import { setPiece, restart } from '../services/TicTacTocService'
const game = new Game(); const game = new Game();
game.players.push(new Player("", 0));
game.players.push(new Player("X", 1)); game.players.push(new Player("X", 1));
game.players.push(new Player("O", 2)); game.players.push(new Player("O", 2));
@ -63,22 +66,6 @@
this.btnText[i.toString()] = game.players[response.board[i]].sign; this.btnText[i.toString()] = game.players[response.board[i]].sign;
} }
} }
/*let btn = $event.target;
//check if filled already
if (this.btnText[btn.id.toString()].length > 0) {
alert("Already filled");
return;
}
//fill X/O
this.btnText[btn.id] = this.game.currentTurn;
this.game.changeTurn();
//check if game won
setTimeout(() => {
if (this.game.checkWinner(btn) == true) {
this.resetGame();
}
}, 100);*/
}, },
resetGame() { resetGame() {
//UI and backend reset //UI and backend reset