got basic commands working

This commit is contained in:
Shuozhe 2021-06-29 15:54:01 +02:00
parent 1ef728dc50
commit ea6ef5b34a
10 changed files with 87 additions and 33 deletions

View File

@ -33,6 +33,14 @@ namespace ProjectGrid.Controllers
return _manager.GetResponse(); return _manager.GetResponse();
} }
[HttpGet]
[Route("api/ttt/Restart")]
public void RestartGame()
{
_logger.LogTrace("RestartGame called.");
_manager.Restart();
}
[HttpPost] [HttpPost]
[Route("api/ttt/SetPiece")] [Route("api/ttt/SetPiece")]
[Consumes("application/json")] [Consumes("application/json")]

View File

@ -29,6 +29,11 @@ namespace ProjectGrid.Models
_field[PosToIdx(x, y)] = value; _field[PosToIdx(x, y)] = value;
_lastSetValue = value; _lastSetValue = value;
} }
private void SetField(int value, int idx)
{
_field[idx] = value;
_lastSetValue = value;
}
// 0 1 2 // 0 1 2
// 3 4 5 // 3 4 5
@ -55,12 +60,13 @@ namespace ProjectGrid.Models
_lastSetValue = 0; _lastSetValue = 0;
} }
public bool SetFieldValue(int value, int x, int y, bool force = false) //public bool SetFieldValue(int value, int x, int y, bool force = false)
public bool SetFieldValue(int value, int idx, bool force = false)
{ {
int field = GetField(x, y); int field = _field[idx];
if (0 == field || force) if (0 == field || force)
{ {
SetField(value, x, y); SetField(value, idx);
return true; return true;
} }
return false; return false;

View File

@ -14,7 +14,7 @@ namespace ProjectGrid
public class TicTacTocManager : ITicTacTocManager public class TicTacTocManager : ITicTacTocManager
{ {
private ITicTacToctRepository _repo; //private ITicTacToctRepository _repo;
private TicTacTocBoard _board; private TicTacTocBoard _board;
public TicTacTocBoard Board => _board; public TicTacTocBoard Board => _board;
@ -22,12 +22,12 @@ namespace ProjectGrid
public TicTacTocManager(/*ITicTacToctRepository repo*/) public TicTacTocManager(/*ITicTacToctRepository repo*/)
{ {
//_repo = repo; //_repo = repo;
_board = new TicTacTocBoard(); Restart();
} }
public bool NextMove(TicTacTocRequest move) public bool NextMove(TicTacTocRequest move)
{ {
return _board.SetFieldValue(move.Player, move.PosX, move.PosY); return _board.SetFieldValue(move.Player, move.Field); //move.PosX, move.PosY);
} }
public TicTacTocResponse GetResponse(int player = 0) public TicTacTocResponse GetResponse(int player = 0)
@ -41,5 +41,10 @@ namespace ProjectGrid
return response; return response;
} }
public void Restart()
{
_board = new TicTacTocBoard();
}
} }
} }

View File

@ -2,12 +2,13 @@ namespace ProjectGrid.Models
{ {
public class TicTacTocRequest public class TicTacTocRequest
{ {
public int PosX { get; set; } //public int PosX { get; set; }
public int PosY { get; set; } //public int PosY { get; set; }
public int Field { get; set; }
public int Player { get; set; } public int Player { get; set; }
public string DebugString() => $"Pos: [{PosX}|{PosY}; Player: {Player}"; public string DebugString() => $"Field: {Field}, Player: {Player}";//$"Pos: [{PosX}|{PosY}; Player: {Player}";
} }
} }

View File

@ -28,6 +28,10 @@
<None Include="$(SpaRoot)**" Exclude="$(SpaRoot)node_modules\**" /> <None Include="$(SpaRoot)**" Exclude="$(SpaRoot)node_modules\**" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Util\" />
</ItemGroup>
<Target Name="DebugEnsureNodeEnv" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('$(SpaRoot)node_modules') "> <Target Name="DebugEnsureNodeEnv" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('$(SpaRoot)node_modules') ">
<!-- Ensure Node.js is installed --> <!-- Ensure Node.js is installed -->
<Exec Command="node --version" ContinueOnError="true"> <Exec Command="node --version" ContinueOnError="true">

View File

@ -17,6 +17,13 @@ class Game {
this._winningConditions = this._winningConditions =
this.winningCondition.map(x => x.join(",")); this.winningCondition.map(x => x.join(","));
} }
makeMove() {
}
changeTurn() { changeTurn() {
this.currentTurn = this.currentTurn == this.players[0].sign ? this.players[1].sign : this.players[0].sign; this.currentTurn = this.currentTurn == this.players[0].sign ? this.players[1].sign : this.players[0].sign;
} }

View File

@ -1,5 +1,6 @@
class Player { class Player {
constructor(sign) { constructor(sign, id) {
this.id = id;
this.sign = sign; this.sign = sign;
this.clicks = []; this.clicks = [];
this.score = 0; this.score = 0;

View File

@ -16,15 +16,15 @@
</div> </div>
<div class="gamebox" style="margin-top:20px"> <div class="gamebox" style="margin-top:20px">
<button id="00" @click="onClick($event)">{{btnText["00"]}}</button> <button id="0" @click="onClick($event)">{{btnText["0"]}}</button>
<button id="01" @click="onClick($event)">{{btnText["01"]}}</button> <button id="1" @click="onClick($event)">{{btnText["1"]}}</button>
<button id="02" @click="onClick($event)">{{btnText["02"]}}</button> <button id="2" @click="onClick($event)">{{btnText["2"]}}</button>
<button id="10" @click="onClick($event)">{{btnText["10"]}}</button> <button id="3" @click="onClick($event)">{{btnText["3"]}}</button>
<button id="11" @click="onClick($event)">{{btnText["11"]}}</button> <button id="4" @click="onClick($event)">{{btnText["4"]}}</button>
<button id="12" @click="onClick($event)">{{btnText["12"]}}</button> <button id="5" @click="onClick($event)">{{btnText["5"]}}</button>
<button id="20" @click="onClick($event)">{{btnText["20"]}}</button> <button id="6" @click="onClick($event)">{{btnText["6"]}}</button>
<button id="21" @click="onClick($event)">{{btnText["21"]}}</button> <button id="7" @click="onClick($event)">{{btnText["7"]}}</button>
<button id="22" @click="onClick($event)">{{btnText["22"]}}</button> <button id="8" @click="onClick($event)">{{btnText["8"]}}</button>
</div> </div>
</div> </div>
</template> </template>
@ -32,10 +32,11 @@
<script> <script>
import Player from "../Player"; import Player from "../Player";
import Game from "../Game"; import Game from "../Game";
import { setPiece, restart } from '../services/TicTacTocService'
const game = new Game(); const game = new Game();
game.players.push(new Player("X")); game.players.push(new Player("X", 1));
game.players.push(new Player("O")); game.players.push(new Player("O", 2));
export default { export default {
name: "GameUI", name: "GameUI",
@ -47,8 +48,23 @@
}; };
}, },
methods: { methods: {
onClick($event) { async onClick($event) {
let btn = $event.target; let btn = $event.target;
const payload = {
Player: 1,
Field: btn.id
}
let response = await setPiece(payload);
console.log(response.board);
if (response.PlayerWon != 0) {
console.log(JSON.stringify(response));
for (let i of [0, 1, 2, 3, 4, 5, 6, 7, 8]) {
this.btnText[i.toString()] = game.players[response.board[i]].sign;
}
}
/*let btn = $event.target;
//check if filled already //check if filled already
if (this.btnText[btn.id.toString()].length > 0) { if (this.btnText[btn.id.toString()].length > 0) {
alert("Already filled"); alert("Already filled");
@ -62,13 +78,15 @@
if (this.game.checkWinner(btn) == true) { if (this.game.checkWinner(btn) == true) {
this.resetGame(); this.resetGame();
} }
}, 100); }, 100);*/
}, },
resetGame() { resetGame() {
//UI and backend reset //UI and backend reset
this.game.reset(); //this.game.reset();
restart();
[...this.buttons].forEach(btn => { [...this.buttons].forEach(btn => {
this.btnText[btn.id] = ""; this.btnText[btn.id] = "";
}); });
} }
}, },

View File

@ -1,11 +1,14 @@
export async function getBoard() { export async function getBoard() {
const response = await fetch('/api/ttt/GetBoard');
const response = await fetch('/api/');
return await response.json(); return await response.json();
} }
export async function createUser(data) { export async function restart() {
const response = await fetch(`/api/user`, { await fetch('/api/ttt/Restart');
}
export async function setPiece(data) {
const response = await fetch(`api/ttt/SetPiece`, {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data) body: JSON.stringify(data)

View File

@ -1,6 +1,7 @@
module.exports = { module.exports = {
devServer: { devServer: {
"public": "vue.hello-world.games", //"public": "vue.hello-world.games",
"public": "localhost",
"port": 10002, "port": 10002,
proxy: { proxy: {
'^/api': { '^/api': {