Game Angular Teste
Game Angular Teste
@Component({
selector: 'app-root',
template: `
<div id="statusArea" className="status">
Next player: <span>{{ nextPlayer }}</span>
</div>
<div id="winnerArea" className="winner">
Winner: <span [attr.class]="winner ? 'winner' : ''">{{ winner }}</span>
</div>
<button (click)="resetGame()" class="button">Reset</button>
<section style="margin-top:10px">
<div class="row" *ngFor="let row of [1, 2, 3]">
<button
*ngFor="let col of [1, 2, 3]"
(click)="play(col, row)"
class="square"
style="width:40px;height:40px;position:relative"
>
<span
class="span"
style="position:absolute; left: 0;
right: 0;
margin:auto"
>{{ arrayPlayers[row - 1][col - 1] }}</span
>
</button>
</div>
</section>
`,
styles: [
`
.winner {
color: white;
font-size: 15px;
font: bold;
background: green;
padding: 5px;
border-radius: 5px;
}
#winnerArea {
margin-top: 5px;
}
.span {
font-size: 15px;
}
`,
],
})
export class AppComponent implements OnInit {
// code goes here
public arrayPlayers: String[][] = [[], [], []];
public newPlayer = <any[]>[];
public winner: string = '';
nextPlayer = <any[]>['X'];
ngOnInit() {}
this.nextPlayer = ['X'];
} else {
if (this.arrayPlayers[row - 1][col - 1]) {
this.nextPlayer = ['X'];
return;
}
this.arrayPlayers[row - 1][col - 1] = 'X';
this.nextPlayer = ['O'];
}
if (
(this.arrayPlayers[0][j] === 'X' &&
this.arrayPlayers[1][j + 1] === 'X' &&
this.arrayPlayers[2][j + 2] === 'X') ||
(this.arrayPlayers[0][j + 2] === 'X' &&
this.arrayPlayers[1][j + 1] === 'X' &&
this.arrayPlayers[2][j] === 'X')
) {
this.winner = 'X Winner';
}
if (
(this.arrayPlayers[0][j] === 'O' &&
this.arrayPlayers[1][j + 1] === 'O' &&
this.arrayPlayers[2][j + 2] === 'O') ||
(this.arrayPlayers[0][j + 2] === 'O' &&
this.arrayPlayers[1][j + 1] === 'O' &&
this.arrayPlayers[2][j] === 'O')
) {
this.winner = 'O Winner';
}
}
}
resetGame() {
this.newPlayer = [];
this.arrayPlayers = [[], [], []];
this.winner = '';
}
}