Skip to content

Commit

Permalink
Update generic version to use foreach loops
Browse files Browse the repository at this point in the history
  • Loading branch information
JanEricNitschke committed Sep 7, 2024
1 parent e9ebb1a commit 9d7773b
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions tictactoe_generic/tictactoe_generic.gen
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ class TicTacToe {

check_win_condition(player, win_condition) {
var result = ConditionResult();
for (var i = 0; i < len(win_condition); i += 1) {
const spot = win_condition[i];
foreach (const spot in win_condition) {
if this.board[spot] == player {
result.spots_done += 1;
}
Expand All @@ -63,8 +62,8 @@ class TicTacToe {
}

is_player_win(player) {
for (var i = 0; i < len(this.win_conditions); i += 1) {
const result = this.check_win_condition(player, this.win_conditions[i]);
foreach (const win_condition in this.win_conditions) {
const result = this.check_win_condition(player, win_condition);
if result.spots_done == 3 {
return true;
}
Expand All @@ -73,8 +72,8 @@ class TicTacToe {
}

board_filled() {
for (var spot = 0; spot < len(this.board); spot += 1) {
unless this.player_markers.contains(this.board[spot]) {
foreach (const spot in this.board) {
unless this.player_markers.contains(spot) {
return false;
}
}
Expand Down Expand Up @@ -130,8 +129,7 @@ class TicTacToe {
}

get_winning_move(player) {
for (var i = 0; i < len(this.win_conditions); i += 1) {
const condition = this.win_conditions[i];
foreach (const condition in this.win_conditions) {
const result = this.check_win_condition(player, condition);
if (result.spots_done == 2 and len(result.spots_open) == 1) {
return Move(result.spots_open[0], 1);
Expand Down Expand Up @@ -183,8 +181,7 @@ class TicTacToe {
}

var best_move = Move(-1, -1);
for (var i = 0; i < len(empty_cells); i += 1) {
const current_spot = empty_cells[i];
foreach (const current_spot in empty_cells) {
this.board[current_spot] = player;
const current_move = this.minmax(this.swap_player(player));
if -current_move.end_state >= best_move.end_state {
Expand Down

0 comments on commit 9d7773b

Please sign in to comment.