Skip to content

Commit

Permalink
Added two player version for perl
Browse files Browse the repository at this point in the history
  • Loading branch information
JanEricNitschke committed May 17, 2024
1 parent e8dbc0a commit 70f33ec
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 1 deletion.
23 changes: 23 additions & 0 deletions .github/workflows/perl.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# This workflow will run stuff on the perl version of tictactoe

name: Perl

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
build:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./tictactoe_perl
steps:
- uses: actions/checkout@v4
- uses: shogo82148/actions-setup-perl@v1
with:
perl-version: "5.38"
- name: Check
run: perl -c tictactoe.pl
13 changes: 12 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,18 @@ repos:
types: [nim]
pass_filenames: false


- repo: https://github.com/perltidy/perltidy
rev: "20240511.02"
hooks:
- id: perltidy
files: ^tictactoe_perl/
types: [perl]


# lua specific lints
# NONE


# swift specific
# NONE

Expand Down Expand Up @@ -252,3 +260,6 @@ repos:

# Julia specific
# None

# C# specific
# None
100 changes: 100 additions & 0 deletions tictactoe_perl/tictactoe.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/usr/bin/perl
use warnings;
use strict;

# Enable autoflush for STDOUT
$| = 1;

my @WINNING_COMBINATIONS = (
[ 0, 1, 2 ], [ 3, 4, 5 ], [ 6, 7, 8 ], # Rows
[ 0, 3, 6 ], [ 1, 4, 7 ], [ 2, 5, 8 ], # Columns
[ 0, 4, 8 ], [ 2, 4, 6 ] # Diagonals
);

sub change_player {
my $player = shift;
return $player eq 'X' ? 'O' : 'X';
}

sub check_draw {
my @board = @{ shift() };
foreach my $cell (@board) {
if ( $cell ne 'X' && $cell ne 'O' ) {
return 0;
}
}
return 1;
}

sub check_win {
my $board = shift;
my $player = shift;

foreach my $combination (@WINNING_COMBINATIONS) {
my $win = 1;
foreach my $cell (@$combination) {
if ( $board->[$cell] ne $player ) {
$win = 0;
last;
}
}
if ($win) {
return 1;
}
}
return 0;
}

sub player_turn {
my $board = shift;
my $player = shift;
my $cell;

while (1) {
print "Player $player, enter your move (0-8): \n";
print_board($board);
$cell = <STDIN>;
chomp $cell;
unless ( $cell =~ /^[0-8]$/ ) {
print "Invalid move. Enter a number between 0 and 8.\n";
next;
}
unless ( $board->[$cell] ne 'X' && $board->[$cell] ne 'O' ) {
print "Invalid move. That space is already taken.\n";
next;
}
last;
}
$board->[$cell] = $player;
}

sub print_board {
my $board = shift;
print " $board->[0] | $board->[1] | $board->[2]\n";
print "---+---+---\n";
print " $board->[3] | $board->[4] | $board->[5]\n";
print "---+---+---\n";
print " $board->[6] | $board->[7] | $board->[8]\n";
}

sub play_game {
my @board = ( 0, 1, 2, 3, 4, 5, 6, 7, 8 );
my $player = 'X';

while (1) {
player_turn( \@board, $player );

if ( check_win( \@board, $player ) ) {
print "Player $player wins!\n";
last;
}
if ( check_draw( \@board ) ) {
print "It's a draw!\n";
last;
}
$player = change_player($player);
}
print_board( \@board );
}

&play_game

0 comments on commit 70f33ec

Please sign in to comment.