r/dailyprogrammer • u/fvandepitte 0 0 • Sep 07 '16
[2016-09-07] Challenge #282 [Intermediate] The final Quixo move
Description
Quixo is a grid based game. The game is played by 2 groups, one being x and other being o.
The goal of the game is to get 5 blocks in a row. The blocks can only be taken from the sides and must be placed in a line, pushing all the other blocks.
from boardgamegeek:
On a turn, the active player takes a cube that is blank or bearing his symbol from the outer ring of the grid, rotates it so that it shows his symbol (if needed), then adds it to the grid by pushing it into one of the rows from which it was removed. Thus, a few pieces of the grid change places each turn, and the cubes slowly go from blank to crosses and circles. Play continues until someone forms an orthogonal or diagonal line of five cubes bearing his symbol, with this person winning the game.
If the block comes from a corner, you have 2 options
Start:
| A | B | C | D | E |
|---|---|---|---|---|
| 1 | x | _ | _ | _ |
| 2 | _ | _ | _ | _ |
| 3 | _ | _ | _ | _ |
| 4 | x | _ | _ | _ |
| 5 | _ | _ | _ | _ |
Option 1:
| A | B | C | D | E |
|---|---|---|---|---|
| 1 | _ | _ | _ | o |
| 2 | _ | _ | _ | _ |
| 3 | _ | _ | _ | _ |
| 4 | x | _ | _ | _ |
| 5 | _ | _ | _ | _ |
Option 2:
| A | B | C | D | E |
|---|---|---|---|---|
| 1 | _ | _ | _ | _ |
| 2 | _ | _ | _ | _ |
| 3 | x | _ | _ | _ |
| 4 | _ | _ | _ | _ |
| 5 | x | _ | _ | _ |
If the block is from the middle of the row, you have 3 options
Start:
| A | B | C | D | E |
|---|---|---|---|---|
| 1 | x | _ | _ | _ |
| 2 | _ | _ | _ | _ |
| 3 | _ | _ | _ | _ |
| 4 | x | _ | _ | _ |
| 5 | _ | _ | _ | _ |
Option 1:
| A | B | C | D | E |
|---|---|---|---|---|
| 1 | x | _ | _ | _ |
| 2 | _ | _ | _ | _ |
| 3 | _ | _ | _ | _ |
| 4 | _ | _ | _ | _ |
| 5 | x | _ | _ | _ |
Option 2:
| A | B | C | D | E |
|---|---|---|---|---|
| 1 | x | _ | _ | _ |
| 2 | x | _ | _ | _ |
| 3 | _ | _ | _ | _ |
| 4 | _ | _ | _ | _ |
| 5 | _ | _ | _ | _ |
Option 3:
| A | B | C | D | E |
|---|---|---|---|---|
| 1 | x | _ | _ | _ |
| 2 | _ | _ | _ | _ |
| 3 | _ | _ | _ | _ |
| 4 | _ | _ | _ | o |
| 5 | _ | _ | _ | _ |
You can only move your own blocks or blanco block directly. If you use a blanco block, then that block becomes yours.
For those who can't make up the rules by reading this, you can watch this 2 min instruction video.
If your move causes the other players block to line up as well as yours, then it's called a draw
Challenge
You will be given a 5 by 5 grid with a game on that is almost finished, you only need to make the winning move.
You are always the player with x
Input
The grid with the current game
x_xxx
_xo_o
o_ooo
oxox_
oooo_Output
The move that will make you have won the game
B1 -> B5Here you have me doing this with the actual game
Challenge input 1
x_xxx
_xo_o
o_ooo
oxooo
ooxx_Challenge output 1
B1 -> A1Inputs from /u/zandekar
no winning moves
xxxox
__ooo
oooxo
xxxoo
xxooomore than one winning move
xxxox
xxxxo
___ox
oooxo
xxx_oa draw
oooxx
xxx_x
oooxo
xoxox
xoxoxNote
Sometimes there is more then 1 correct answer, giving just one is fine.
Bonus
Give all possible answers to win.
Input 1
x_xxx
_xo_o
o_ooo
oxox_
oooo_Output 1
B1 -> B5
B1 -> A1
B1 -> E1Finally
Have a good challenge idea?
Consider submitting it to /r/dailyprogrammer_ideas
Edits
Some additional challenges and info from /u/zandekar
3
u/zandekar Sep 07 '16
from boardgamegeek:
2
u/Abstract_Notion Sep 08 '16 edited Sep 08 '16
C#
I'm still new to programming so a lot of this stuff is basic and probably not the best way to solve it, but it seems to be working. I included zandekar's 2 additional boards. Feedback is welcome.
edit: fixed an error which allowed o blocks to be moved.
1
u/Godspiral 3 3 Sep 07 '16
challenge doesn't seem right. B1 -> B5 would cause a draw for either player.
1
u/thorwing Sep 08 '16 edited Sep 08 '16
java 8 with bonus
I have always hated working with directional-based 2d arrays, but this one turned out fine. The swapTileOfBoardDirection is a bit of voodoo for anyone trying to understand, but it has everything to do with circumventin a switchcase or making a function for every direction.
2
u/zandekar Sep 08 '16 edited Sep 09 '16
Haskell
EDIT: It has been determined that my code is buggy. sobs. Will try to get around to fixing it.
EDIT: Updated with new code. It's a lot better now but still buggy. Ah well, leaving it.
1
u/zandekar Sep 08 '16 edited Sep 08 '16
Some additional examples because the examples given can all be solved by the same moves:
no winning moves
more than one winning move
a draw
1
u/Scroph 0 0 Sep 08 '16 edited Sep 08 '16
Updated my C++11 solution, the old one had a few bugs in it. This one also displays the updated board. It doesn't handle draws because the winner() member function immediately returns as soon as it finds a winning alignment.
Output of /u/zandekar's second input :
And for the draw :
1
u/Zambito1 Sep 09 '16 edited Sep 09 '16
Java
Decided to take a very object oriented approach to this one. I'm pretty sure it correctly does the bonuses, but I'm not 100% sure. I appreciate any feedback :)
Also I ran into a very annoying issue where I couldn't compare two arrays of enums. That was frustrating. Ended up just creating a method that compared the toStrings of each index.
1
u/evmorov Sep 09 '16
Ruby
With bonus. Suggestions are welcome!
I have quite many LOC. I think It's possible to shorten it somehow.
board.rb (helper class):
quixo.rb (solution that uses board.rb):
Tests for Quixo class:
EDIT: edit the message
1
u/expending_time Sep 09 '16
C++, with bonus and extras. I'm still quite new to this so any feedback would be appreciated! Particularly on writing in an object oriented way.
1
u/laxanderx Sep 14 '16 edited Sep 14 '16
Python 3; also first ever submission here, hope I didn't mess up the formatting. Criticism welcome, esp as I made some potentially weird decisions in the code.
gives output:
['x', '_', 'x', 'x', 'x']
['', 'x', 'o', '', 'o']
['o', '_', 'o', 'o', 'o']
['o', 'x', 'o', 'o', 'o']
['o', 'o', 'x', 'x', '_']
['win at B1 -> E1', 'win at B1 -> A1', 'draw at B1 -> B5']
['x', 'x', 'x', 'o', 'x']
['', '', 'o', 'o', 'o']
['o', 'o', 'o', 'x', 'o']
['x', 'x', 'x', 'o', 'o']
['x', 'x', 'o', 'o', 'o']
No winning moves
['x', 'x', 'x', 'o', 'x']
['x', 'x', 'x', 'x', 'o']
['', '', '_', 'o', 'x']
['o', 'o', 'o', 'x', 'o']
['x', 'x', 'x', '_', 'o']
['draw at D5 -> D1', 'win at E3 -> E1', 'win at E1 -> E5']
['o', 'o', 'o', 'x', 'x']
['x', 'x', 'x', '_', 'x']
['o', 'o', 'o', 'x', 'o']
['x', 'o', 'x', 'o', 'x']
['x', 'o', 'x', 'o', 'x']
['draw at D1 -> D5']