perl logo Perl logo (Thanks to Olaf Alders)

The weekly challenge 335 - Task 2: Find Winner

  1 #!/usr/bin/env perl
  2 # https://theweeklychallenge.org/blog/perl-weekly-challenge-335/#TASK2
  3 #
  4 # Task 2: Find Winner
  5 # ===================
  6 #
  7 # You are given an array of all moves by the two players.
  8 #
  9 # Write a script to find the winner of the TicTacToe game if found based on the
 10 # moves provided in the given array.
 11 #
 12 # UPDATE: Order move is in the order - A, B, A, B, A, ….
 13 #
 14 ## Example 1
 15 ##
 16 ## Input: @moves = ([0,0],[2,0],[1,1],[2,1],[2,2])
 17 ## Output: A
 18 ##
 19 ## Game Board:
 20 ## [ A _ _ ]
 21 ## [ B A B ]
 22 ## [ _ _ A ]
 23 #
 24 # Note: Example 1 has a typo either in the list of moves (second move
 25 # should be [1,0]) or in the output Game Board (B in line 1 column 0
 26 # should move to line 2, column 0). I just use both alternatives below
 27 #
 28 ## Example 2
 29 ##
 30 ## Input: @moves = ([0,0],[1,1],[0,1],[0,2],[1,0],[2,0])
 31 ## Output: B
 32 ##
 33 ## Game Board:
 34 ## [ A A B ]
 35 ## [ A B _ ]
 36 ## [ B _ _ ]
 37 #
 38 #
 39 ## Example 3
 40 ##
 41 ## Input: @moves = ([0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2])
 42 ## Output: Draw
 43 ##
 44 ## Game Board:
 45 ## [ A A B ]
 46 ## [ B B A ]
 47 ## [ A B A ]
 48 #
 49 #
 50 ## Example 4
 51 ##
 52 ## Input: @moves = ([0,0],[1,1])
 53 ## Output: Pending
 54 ##
 55 ## Game Board:
 56 ## [ A _ _ ]
 57 ## [ _ B _ ]
 58 ## [ _ _ _ ]
 59 #
 60 #
 61 ## Example 5
 62 ##
 63 ## Input: @moves = ([1,1],[0,0],[2,2],[0,1],[1,0],[0,2])
 64 ## Output: B
 65 ##
 66 ## Game Board:
 67 ## [ B B B ]
 68 ## [ A A _ ]
 69 ## [ _ _ A ]
 70 #
 71 ############################################################
 72 ##
 73 ## discussion
 74 ##
 75 ############################################################
 76 #
 77 # Let's first initialize an empty board. Then we replace the fields
 78 # corresponding to a move with the corresponding character.
 79 # Now we have to check the board - all rows, columns and diagonals.
 80 # If we find a winning position for any of the players, return the
 81 # result. If we don't, then we return "Draw" if all fields in the
 82 # board have been filled, otherwise we return "Pending".
 83 
 84 use v5.36;
 85 
 86 find_winner([0,0],[2,0],[1,1],[2,1],[2,2]);
 87 find_winner([0,0],[1,0],[1,1],[2,1],[2,2]);
 88 find_winner([0,0],[1,1],[0,1],[0,2],[1,0],[2,0]);
 89 find_winner([0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]);
 90 find_winner([0,0],[1,1]);
 91 find_winner([1,1],[0,0],[2,2],[0,1],[1,0],[0,2]);
 92 
 93 
 94 sub find_winner( @moves ) {
 95     say "Input: (" . join(", ", map {"[$_->[0],$_->[1]]"} @moves) . ")";
 96     my $board = [ ["_", "_", "_"], ["_", "_", "_"], ["_", "_", "_"] ];
 97     my $next = "A";
 98     foreach my $move (@moves) {
 99         $board->[$move->[0]]->[$move->[1]] = $next;
100         $next = $next eq "A" ? "B" : "A";
101     }
102     my $winner = "Pending";
103     my $is_filled = 1;
104     foreach my $x (0..2) {
105         foreach my $y (0..2) {
106             $is_filled = 0 if $board->[$x]->[$y] eq "_";
107         }
108     }
109     # let's check the rows
110     foreach my $x (0..2) {
111         if($board->[$x]->[0] eq $board->[$x]->[1] && $board->[$x]->[1] eq $board->[$x]->[2] && $board->[$x]->[0] ne "_") {
112             return say "Output: $board->[$x]->[0]";
113         }
114     }
115     # let's check the columns
116     foreach my $y (0..2) {
117         if($board->[0]->[$y] eq $board->[1]->[$y] && $board->[1]->[$y] eq $board->[2]->[$y] && $board->[0]->[$y] ne "_") {
118             return say "Output: $board->[0]->[$y]";
119         }
120     }
121     # let's check the diagonals
122     if($board->[0]->[0] eq $board->[1]->[1] && $board->[1]->[1] eq $board->[2]->[2] && $board->[1]->[1] ne "_") {
123         return say "Output: $board->[0]->[0]";
124     }
125     if($board->[2]->[0] eq $board->[1]->[1] && $board->[1]->[1] eq $board->[0]->[2] && $board->[1]->[1] ne "_") {
126         return say "Output: $board->[2]->[0]";
127     }
128     if($is_filled) {
129         $winner = "Draw";
130     }
131     say "Output: $winner";
132 }