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();
}
[HttpGet]
[Route("api/ttt/Restart")]
public void RestartGame()
{
_logger.LogTrace("RestartGame called.");
_manager.Restart();
}
[HttpPost]
[Route("api/ttt/SetPiece")]
[Consumes("application/json")]

View File

@ -29,6 +29,11 @@ namespace ProjectGrid.Models
_field[PosToIdx(x, y)] = value;
_lastSetValue = value;
}
private void SetField(int value, int idx)
{
_field[idx] = value;
_lastSetValue = value;
}
// 0 1 2
// 3 4 5
@ -55,12 +60,13 @@ namespace ProjectGrid.Models
_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)
{
SetField(value, x, y);
SetField(value, idx);
return true;
}
return false;

View File

@ -14,7 +14,7 @@ namespace ProjectGrid
public class TicTacTocManager : ITicTacTocManager
{
private ITicTacToctRepository _repo;
//private ITicTacToctRepository _repo;
private TicTacTocBoard _board;
public TicTacTocBoard Board => _board;
@ -22,12 +22,12 @@ namespace ProjectGrid
public TicTacTocManager(/*ITicTacToctRepository repo*/)
{
//_repo = repo;
_board = new TicTacTocBoard();
Restart();
}
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)
@ -41,5 +41,10 @@ namespace ProjectGrid
return response;
}
public void Restart()
{
_board = new TicTacTocBoard();
}
}
}

View File

@ -2,12 +2,13 @@ namespace ProjectGrid.Models
{
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 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\**" />
</ItemGroup>
<ItemGroup>
<Folder Include="Util\" />
</ItemGroup>
<Target Name="DebugEnsureNodeEnv" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('$(SpaRoot)node_modules') ">
<!-- Ensure Node.js is installed -->
<Exec Command="node --version" ContinueOnError="true">

View File

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

View File

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

View File

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

View File

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

View File

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