Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save vext01/7153860 to your computer and use it in GitHub Desktop.

Select an option

Save vext01/7153860 to your computer and use it in GitHub Desktop.
From b01b0f22245fe5f0d527b31f4150d9c82f18cea2 Mon Sep 17 00:00:00 2001
From: Edd Barrett <vext01@gmail.com>
Date: Fri, 25 Oct 2013 12:49:56 +0100
Subject: [PATCH] Make connect 4 AI non-deterministic (in the random sense).
---
connect4/connect4.pl | 11 ++++++++++-
connect4/gui.py | 10 +++++++++-
2 files changed, 19 insertions(+), 2 deletions(-)
diff --git a/connect4/connect4.pl b/connect4/connect4.pl
index 9b16136..57ca7bf 100644
--- a/connect4/connect4.pl
+++ b/connect4/connect4.pl
@@ -83,12 +83,21 @@ insert_token(pos(Reds, Yellows, WhoseMove), Col, Move) :-
pos(Reds, [c(Col, Y) | Yellows], red),
WhoseMove, Move).
+% Nasty hack to work around lack of proper list conversion
+% over the language boundary. XXX
+shuffle(In, Out) :-
+ Hack =.. [xxx | In ],
+ python:shuffle_moves(Hack, Shuffled),
+ Shuffled =.. [ _ | Out ].
+
% Find all possible subsequent game states
moves(pos(Reds, Yellows, _), []) :- % if someone won, dont collect moves
has_won(Reds, Yellows, _), !.
+
moves(Pos, Moves) :-
board_width(Width), Col is Width - 1,
- findall(Move, moves(Pos, Move, Col), Moves).
+ findall(Move, moves(Pos, Move, Col), MovesOrdered),
+ shuffle(MovesOrdered, Moves).
moves(pos(Reds, Yellows, WhoseMove), Move, Col) :-
board_width(W), Col < W, Col > -1,
diff --git a/connect4/gui.py b/connect4/gui.py
index 4c20b3e..75a9a58 100644
--- a/connect4/gui.py
+++ b/connect4/gui.py
@@ -1,9 +1,17 @@
import Tkinter as tk
-import uni, sys
+import uni, sys, random
ROWS = 6
COLS = 7
+# Since there is no RNG is pyrolog, we let Python shuffle our list
+# XXX No proper list conversion from Prolog, so the list comes in as a
+# n-ary term, which we then shuffle and throw back.
+def shuffle_moves(moves_term):
+ l = list(moves_term)
+ random.shuffle(l)
+ return uni.CoreTerm("xxx", l)
+
def token_click_closure(c4, colno):
return lambda : c4._player_turn(colno)
--
1.8.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment