5 kyu

Pigs in a Pen

20 of 317adrian.eyre

Description:

Introduction

Dots and Boxes is a pencil-and-paper game for two players (sometimes more). It was first published in the 19th century by Édouard Lucas, who called it la pipopipette. It has gone by many other names, including the game of dots, boxes, dot to dot grid, and pigs in a pen.

Starting with an empty grid of dots, two players take turns adding a single horizontal or vertical line between two unjoined adjacent dots. The player who completes the fourth side of a 1×1 box earns one point and takes another turn only if another box can be made. (A point is typically recorded by placing a mark that identifies the player in the box, such as an initial). The game ends when no more lines can be placed. The winner is the player with the most points. The board may be of any size. When short on time, a 2×2 board (a square of 9 dots) is good for beginners. A 5×5 is good for experts. (Source Wikipedia)
Squares

Task

Your task is to complete the class called Game. You will be given the board size as an integer board that will be between 1 and 26, therefore the game size will be board x board. You will be given an array of lines that have already been taken, so you must complete all possible squares.

Rules

1.  The integer board will be passed when the class is initialised.

2.  board will be between 1 and 26.

3.  The lines array maybe empty or contain a list of line integers.

4.  You can only complete a square if 3 out of the 4 sides are already complete.

5.  The lines array that is passed into the play() function may not be sorted numerically!

Returns

Return an array of all the lines filled in including the original lines.

Return array must be sorted numerically.

Return array must not contain duplicate integers.

Example 1

Initialise

Initialise a board of 2 squares by 2 squares where board = 2

Line Numbering

Line Input

So for the line input of [1, 3, 4] the below lines would be complete

to complete the square line 6 is needed

Game Play

board = 2
lines = [1, 3, 4]
game = Game.new(board)
game.play(lines) => [1, 3, 4, 6]
board = 2
lines = [1, 3, 4]
game = Game(board)
game.play(lines) => [1, 3, 4, 6]
board = 2;
lines = [1, 3, 4];
game = new Game(board);
game.play(lines) => [1, 3, 4, 6]
int board = 2;
List<int> lines = new List<int> { 1, 3, 4 };
Game game = new Game(board);
game.play(lines) => { 1, 3, 4, 6 }

Example 2

Initialise

board = 2
lines = [1, 2, 3, 4, 5, 8, 10, 11, 12]
game = Game.new(board)
game.play(lines) => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
board = 2
lines = [1, 2, 3, 4, 5, 8, 10, 11, 12]
game = Game.new(board)
game.play(lines) => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
board = 2;
lines = [1, 2, 3, 4, 5, 8, 10, 11, 12];
game = Game.new(board);
game.play(lines) => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
int board = 2;
List<int> lines = new List<int> { 1, 2, 3, 4, 5, 8, 10, 11, 12 };
Game game = new Game(board);
game.play(lines) => { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }

Solution


Good luck and enjoy!

Kata Series

If you enjoyed this, then please try one of my other Katas. Any feedback, translations and grading of beta Katas are greatly appreciated. Thank you.

Rank Maze Runner

Rank Scooby Doo Puzzle

Rank Driving License

Rank Connect 4

Rank Vending Machine

Rank Snakes and Ladders

Rank Mastermind

Rank Guess Who?

Rank Am I safe to drive?

Rank Mexican Wave

Rank Pigs in a Pen

Rank Hungry Hippos

Rank Plenty of Fish in the Pond

Rank Fruit Machine

Rank Car Park Escape

Puzzles
Arrays
Fundamentals

Stats:

CreatedApr 24, 2017
PublishedApr 24, 2017
Warriors Trained2391
Total Skips890
Total Code Submissions3991
Total Times Completed317
Ruby Completions20
Python Completions126
JavaScript Completions90
C# Completions52
PHP Completions20
TypeScript Completions27
Total Stars143
% of votes with a positive feedback rating97% of 100
Total "Very Satisfied" Votes94
Total "Somewhat Satisfied" Votes6
Total "Not Satisfied" Votes0
Total Rank Assessments8
Average Assessed Rank
5 kyu
Highest Assessed Rank
4 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • adrian.eyre Avatar
  • Blind4Basics Avatar
  • hobovsky Avatar
Ad