I am looking for an algorithm to detect an early tie in a Connect 4 game. As of now, I already check if the board is full and no wins have been detected but I would like to know as soon as the game can be deduced a tie.
For example, consider this game, where player B just played at position Row-5, Column-0:
|
v
5 | B | | | | | | |
4 | A | B | A | B | A | B | A |
3 | A | B | A | B | A | B | A |
2 | B | A | B | A | B | A | B |
1 | B | A | B | A | B | A | B |
0 | A | B | A | B | A | B | A |
0 1 2 3 4 5 6
Then, the game is not considered a tie, because there is still a way for player B to win. Though if player A plays at Row-5, Column-1:
|
v
5 | B | A | | | | | |
4 | A | B | A | B | A | B | A |
3 | A | B | A | B | A | B | A |
2 | B | A | B | A | B | A | B |
1 | B | A | B | A | B | A | B |
0 | A | B | A | B | A | B | A |
0 1 2 3 4 5 6
At this point, either player has no way to win: it going to be a tie. I would like the algorithm to notify the user of this right away.
Check all possible runs of 4-in-a-row on the board, horizontal, vertical and diagonal. If all of them contain at least one A and at least one B then it's going to be a tie. If there is even one that is made up of a combination of empty and A or empty and B (assuming there are no rows of 4-in-a-row A or 4-in-a-row B, in which case you already have a win), then a win by A or B is still possible.
You probably already have code to check for a win, so just adapt it to check for 4-in-a-row of A or empty, or 4-in-a-row of B or empty instead of 4-in-a-row of A, or 4-in-a-row of B. If it fails to detect a possible win then a tie is inevitable.
One wrinkle is when there are a small number of empty spaces on the board. In this case you need to work out how many remaining moves A and B have and only allow them that many number of empties in the calculation, e.g. check for 4-in-a-row of A and a maximum of 2 empties. For example if there are 5 empty spaces left and it's B's turn then A has 2 moves left and B has 3.
One case it won't handle is if there is a single empty column left - there might be enough space for one player to stack 4 in a row but they can't because the players have to alternate.
I just stumbled upon a strange (and very annoying game) that I wanted to solve programmatically. It reminds a bit of Rubik's cube, but 2 dimensional. I'm struggling a bit on how to approach this...
There is a 9x9 square with some circles placed into the inner squares. For instance, one get's the following picture:
A B C D E F G H I
-------------------------------------
9 | | | O | | | O | | | | J
-------------------------------------
8 | | | O | | O | | O | | | K
-------------------------------------
7 | | | | O | | | O | O | | L
-------------------------------------
6 | | | O | | | | O | | | M
-------------------------------------
5 | | | O | | | | | | | N
-------------------------------------
4 | | | | O | | O | O | | | O
-------------------------------------
3 | | | | | O | | O | | | P
-------------------------------------
2 | | | | O | | | | | | Q
-------------------------------------
1 | | | O | | | | | | | R
-------------------------------------
0 Z Y X W V U T S
One can use the numbers and letters arround the square to shift entire "rows" or "columns" to either left/right or up/down. Circles that would leave the game area to the right would reappear on the left and vise-versa, same accounts for top/bottom.
The goal is to rearrange the circles to a given pattern with a maximum amount of moves. For instance, one should rearrange the circles in the above picture to reflect the below picture in maximum 17 moves:
A B C D E F G H I
-------------------------------------
9 | | | | | | | | | | J
-------------------------------------
8 | | | O | O | O | O | O | | | K
-------------------------------------
7 | | | O | | | | O | | | L
-------------------------------------
6 | | | O | | | | O | | | M
-------------------------------------
5 | | | O | | | | O | | | N
-------------------------------------
4 | | | O | | | | O | | | O
-------------------------------------
3 | | | O | O | O | O | O | | | P
-------------------------------------
2 | | | | | | | | | | Q
-------------------------------------
1 | | | | | | | | | | R
-------------------------------------
0 Z Y X W V U T S
I would like to feed the starting and the end position of the circles to a program that delivers the shortest path possible. I'm struggling a bit to find an approach that doesn't just try all possible moves until a given maximum number of moves is reached.
Also it doesn't seem to be that easy to modify the approach that's being used to solve a Rubik's cube for instance...
Well, I thought it was a very interesting problem, and maybe somebody here has an illuminating idea.
UPDATE:
Just trying all the possible moves doesn't really seem realistic after a first try. There are just too many permutations. I think this could be really hard to solve...if possible at all.
I'm trying to determine how a Turing Machine (consisting of only 0's and 1's, no blanks) could recognize a sequence of 8 1's. Every algorithm I've found has a TM searching for an indeterminate number of 1's or 0's, not a specific number.
Essentially, if you have this tape:
1 1 1 1 1 1 1 1 0 0 0 1 0 0 0 1
How can you recognize that the 8 1's represent addition, and you want to add 0 0 0 1 and 0 0 0 1?
I take it that 11111111 is like an opcode and 0001, 0001 are the operands for that opcode. At least, that's the only interpretation I am seeing.
A TM can look for a fixed, finite number of symbols by using a similar fixed, finite number of states, the sole purpose of each one being to recognize that another of the expected symbols has been seen. For instance, here's a four-tape TM that recognizes addition and does the binary addition:
|----|----|----|----|----||----|-----|-----|-----|-----|----|----|----|----|
| Q | T1 | T2 | T3 | T4 || Q' | T1' | T2' | T3' | T4' | D1 | D2 | D3 | D4 |
|----|----|----|----|----||----|-----|-----|-----|-----|----|----|----|----|
// read the opcode /////////////////////////////////////////////////////////
| qA | 1 | x | y | z || qB | 1 | x | y | z | R | S | S | S |
| qB | 1 | x | y | z || qC | 1 | x | y | z | R | S | S | S |
| qC | 1 | x | y | z || qD | 1 | x | y | z | R | S | S | S |
| qD | 1 | x | y | z || qE | 1 | x | y | z | R | S | S | S |
| qE | 1 | x | y | z || qF | 1 | x | y | z | R | S | S | S |
| qF | 1 | x | y | z || qG | 1 | x | y | z | R | S | S | S |
| qG | 1 | x | y | z || qH | 1 | x | y | z | R | S | S | S |
| qH | 1 | x | y | z || qI | 1 | x | y | z | R | S | S | S |
// read the first addend ///////////////////////////////////////////////////
| qI | w | x | y | z || qJ | w | w | y | z | R | R | S | S |
| qJ | w | x | y | z || qK | w | w | y | z | R | R | S | S |
| qK | w | x | y | z || qL | w | w | y | z | R | R | S | S |
| qL | w | x | y | z || qM | w | w | y | z | R | R | S | S |
// read the second addend //////////////////////////////////////////////////
| qM | w | x | y | z || qN | w | x | w | z | R | S | R | S |
| qN | w | x | y | z || qO | w | x | w | z | R | S | R | S |
| qO | w | x | y | z || qP | w | x | w | z | R | S | R | S |
| qP | w | x | y | z || qQ | w | x | w | z | R | S | R | S |
// prepare the output tape /////////////////////////////////////////////////
| qQ | w | x | y | z || qR | w | x | y | z | S | S | S | R |
| qR | w | x | y | z || qS | w | x | y | z | S | S | S | R |
| qS | w | x | y | z || qT | w | x | y | z | S | S | S | R |
| qT | w | x | y | z || qU | w | x | y | z | S | S | S | R |
// handle addition when no carry ///////////////////////////////////////////
| qU | w | 0 | 0 | z || qU | w | 0 | 0 | 0 | S | L | L | L |
| qU | w | 0 | 1 | z || qU | w | 0 | 1 | 1 | S | L | L | L |
| qU | w | 1 | 0 | z || qU | w | 1 | 0 | 1 | S | L | L | L |
| qU | w | 1 | 1 | z || qV | w | 1 | 1 | 0 | S | L | L | L |
| qU | w | B | B | B || hA | w | B | B | B | S | R | R | R |
// handle addition when carry //////////////////////////////////////////////
| qV | w | 0 | 0 | z || qU | w | 0 | 0 | 1 | S | L | L | L |
| qV | w | 0 | 1 | z || qV | w | 0 | 1 | 0 | S | L | L | L |
| qV | w | 1 | 0 | z || qV | w | 1 | 0 | 0 | S | L | L | L |
| qV | w | 1 | 1 | z || qV | w | 1 | 1 | 1 | S | L | L | L |
| qV | w | B | B | B || hA | w | B | B | B | S | R | R | R |
|----|----|----|----|----||----|-----|-----|-----|-----|----|----|----|----|
Legend:
Q: current state
T1: current tape symbol, input tape
T2: current tape symbol, scratch tape #1
T3: current tape symbol, scratch tape #2
T4: current tape symbol, output tape (not used)
Q': state to transition into
T1': symbol to write to input tape (not used)
T2': symbol to write to scratch tape #1
T3': symbol to write to scratch tape #2
T4': symbol to write to output tape
D1: direction to move input tape head
D2: direction to move scratch tape #1 head
D3: direction to move scratch tape #2 head
D4: direction to move output tape head
Conventions:
w, x, y and z are variables and represent either 0 or 1. A transition using all four of these can be thought of as a shorthand notation for writing sixteen (2^4) concrete transitions.
directions are L=left, S=same, R=right.
B is a blank symbol; it can be dispensed with if you add more states to assist U and V in the addition.
Given two sets, e.g.:
{A B C}, {1 2 3 4 5 6}
I want to generate the Cartesian product in an order that puts as much space as possible between equal elements. For example, [A1, A2, A3, A4, A5, A6, B1…] is no good because all the As are next to each other. An acceptable solution would be going "down the diagonals" and then every time it wraps offsetting by one, e.g.:
[A1, B2, C3, A4, B5, C6, A2, B3, C4, A5, B6, C1, A3…]
Expressed visually:
| | A | B | C | A | B | C | A | B | C | A | B | C | A | B | C | A | B | C |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 1 | | | | | | | | | | | | | | | | | |
| 2 | | 2 | | | | | | | | | | | | | | | | |
| 3 | | | 3 | | | | | | | | | | | | | | | |
| 4 | | | | 4 | | | | | | | | | | | | | | |
| 5 | | | | | 5 | | | | | | | | | | | | | |
| 6 | | | | | | 6 | | | | | | | | | | | | |
| 1 | | | | | | | | | | | | | | | | | | |
| 2 | | | | | | | 7 | | | | | | | | | | | |
| 3 | | | | | | | | 8 | | | | | | | | | | |
| 4 | | | | | | | | | 9 | | | | | | | | | |
| 5 | | | | | | | | | | 10| | | | | | | | |
| 6 | | | | | | | | | | | 11| | | | | | | |
| 1 | | | | | | | | | | | | 12| | | | | | |
| 2 | | | | | | | | | | | | | | | | | | |
| 3 | | | | | | | | | | | | | 13| | | | | |
| 4 | | | | | | | | | | | | | | 14| | | | |
| 5 | | | | | | | | | | | | | | | 15| | | |
| 6 | | | | | | | | | | | | | | | | 16| | |
| 1 | | | | | | | | | | | | | | | | | 17| |
| 2 | | | | | | | | | | | | | | | | | | 18|
or, equivalently but without repeating the rows/columns:
| | A | B | C |
|---|----|----|----|
| 1 | 1 | 17 | 15 |
| 2 | 4 | 2 | 18 |
| 3 | 7 | 5 | 3 |
| 4 | 10 | 8 | 6 |
| 5 | 13 | 11 | 9 |
| 6 | 16 | 14 | 12 |
I imagine there are other solutions too, but that's the one I found easiest to think about. But I've been banging my head against the wall trying to figure out how to express it generically—it's a convenient thing that the cardinality of the two sets are multiples of each other, but I want the algorithm to do The Right Thing for sets of, say, size 5 and 7. Or size 12 and 69 (that's a real example!).
Are there any established algorithms for this? I keep getting distracted thinking of how rational numbers are mapped onto the set of natural numbers (to prove that they're countable), but the path it takes through ℕ×ℕ doesn't work for this case.
It so happens the application is being written in Ruby, but I don't care about the language. Pseudocode, Ruby, Python, Java, Clojure, Javascript, CL, a paragraph in English—choose your favorite.
Proof-of-concept solution in Python (soon to be ported to Ruby and hooked up with Rails):
import sys
letters = sys.argv[1]
MAX_NUM = 6
letter_pos = 0
for i in xrange(MAX_NUM):
for j in xrange(len(letters)):
num = ((i + j) % MAX_NUM) + 1
symbol = letters[letter_pos % len(letters)]
print "[%s %s]"%(symbol, num)
letter_pos += 1
String letters = "ABC";
int MAX_NUM = 6;
int letterPos = 0;
for (int i=0; i < MAX_NUM; ++i) {
for (int j=0; j < MAX_NUM; ++j) {
int num = ((i + j) % MAX_NUM) + 1;
char symbol = letters.charAt(letterPos % letters.length);
String output = symbol + "" + num;
++letterPos;
}
}
What about using something fractal/recursive? This implementation divides a rectangular range into four quadrants then yields points from each quadrant. This means that neighboring points in the sequence differ at least by quadrant.
#python3
import sys
import itertools
def interleave(*iters):
for elements in itertools.zip_longest(*iters):
for element in elements:
if element != None:
yield element
def scramblerange(begin, end):
width = end - begin
if width == 1:
yield begin
else:
first = scramblerange(begin, int(begin + width/2))
second = scramblerange(int(begin + width/2), end)
yield from interleave(first, second)
def scramblerectrange(top=0, left=0, bottom=1, right=1, width=None, height=None):
if width != None and height != None:
yield from scramblerectrange(bottom=height, right=width)
raise StopIteration
if right - left == 1:
if bottom - top == 1:
yield (left, top)
else:
for y in scramblerange(top, bottom):
yield (left, y)
else:
if bottom - top == 1:
for x in scramblerange(left, right):
yield (x, top)
else:
halfx = int(left + (right - left)/2)
halfy = int(top + (bottom - top)/2)
quadrants = [
scramblerectrange(top=top, left=left, bottom=halfy, right=halfx),
reversed(list(scramblerectrange(top=top, left=halfx, bottom=halfy, right=right))),
scramblerectrange(top=halfy, left=left, bottom=bottom, right=halfx),
reversed(list(scramblerectrange(top=halfy, left=halfx, bottom=bottom, right=right)))
]
yield from interleave(*quadrants)
if __name__ == '__main__':
letters = 'abcdefghijklmnopqrstuvwxyz'
output = []
indices = dict()
for i, pt in enumerate(scramblerectrange(width=11, height=5)):
indices[pt] = i
x, y = pt
output.append(letters[x] + str(y))
table = [[indices[x,y] for x in range(11)] for y in range(5)]
print(', '.join(output))
print()
pad = lambda i: ' ' * (2 - len(str(i))) + str(i)
header = ' |' + ' '.join(map(pad, letters[:11]))
print(header)
print('-' * len(header))
for y, row in enumerate(table):
print(pad(y)+'|', ' '.join(map(pad, row)))
Outputs:
a0, i1, a2, i3, e0, h1, e2, g4, a1, i0, a3, k3, e1,
h0, d4, g3, b0, j1, b2, i4, d0, g1, d2, h4, b1, j0,
b3, k4, d1, g0, d3, f4, c0, k1, c2, i2, c1, f1, a4,
h2, k0, e4, j3, f0, b4, h3, c4, j2, e3, g2, c3, j4,
f3, k2, f2
| a b c d e f g h i j k
-----------------------------------
0| 0 16 32 20 4 43 29 13 9 25 40
1| 8 24 36 28 12 37 21 5 1 17 33
2| 2 18 34 22 6 54 49 39 35 47 53
3| 10 26 50 30 48 52 15 45 3 42 11
4| 38 44 46 14 41 31 7 23 19 51 27
If your sets X and Y are sizes m and n, and Xi is the index of the element from X that's in the ith pair in your Cartesian product (and similar for Y), then
Xi = i mod n;
Yi = (i mod n + i div n) mod m;
You could get your diagonals a little more spread out by filling out your matrix like this:
for (int i = 0; i < m*n; i++) {
int xi = i % n;
int yi = i % m;
while (matrix[yi][xi] != 0) {
yi = (yi+1) % m;
}
matrix[yi][xi] = i+1;
}
I have an interesting programming problem that I need to solve for a iPhone app that I am currently building. The problem is actually a logic problem that does not need to be specific to any particular programming language.
The app needs to produce a linkages map (apologies if this isn't the right terminology but it makes sense to me). You have the following data:
A=C
B=A
C=O
D=F
E=F
F=G
G=D
H=J
I=L
J=N
K=A
L=O
M=C
N=H
O=E
The letters A through to O can be linked to any other letter. The app needs to follow the links to create a map, so starting with A, A link to C, C link to O, O links to E, E links to F etc
When complete this map would look like the attached photo.
http://i.stack.imgur.com/TEfAs.jpg
The problem I have is that I need to write code that will output any map using any combination of links. So for example another link list might look like
A=B
B=A
C=A
D=A
E=A
F=A
G=A
H=A
I=A
J=A
K=A
L=A
M=A
N=A
O=A
I can't get my head around the pseudocode / logic for drawing the app. There are always 15 letters A-O and a letter can never be linked to itself so A can never = A.
Can anyone help to come up with the logic for drawing the map?
What you want is to draw a graph. There is no canonical graphical representation of a graph. So if you have no constrains how the graph should be drawn, you can simply make a row of the Letters and than draw arches between the letters according to your map,
Little like this (ASCII-ART):
Example
+-----------------------------------------+
+--------------------------------------+ |
+-----------------------------------+ | |
+--------------------------------+ | | |
+-----------------------------+ | | | |
+--------------------------+ | | | | |
+-----------------------+ | | | | | |
+--------------------+ | | | | | | |
+-----------------+ | | | | | | | |
+--------------+ | | | | | | | | |
+-----------+ | | | | | | | | | |
+--------+ | | | | | | | | | | |
+-----+ | | | | | | | | | | | |
| | | | | | | | | | | | | |
A B C D E F G H I J K L M N O
| |
+--+
Example
+-----------------------------+
+-----------------------------+ |
+--+ +-----------------------------------+
| | | | +--------+ | |
A B C D E F G H I J K L M N O
| | | | | | | | | | | |
+-----+ | +--+ | +-----+ | +--------+
| +-----+ | | +-----------+
| | +--+ +-----------------+
| +--------+ |
+-----------------------------+
Look a bit confusing, but you cannot always avoid crossings. [In this example you could, but I did not try to avoid crossing, because they cannot be avoided in the general case.]