-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpySolitarie.py
More file actions
executable file
·127 lines (106 loc) · 3.44 KB
/
pySolitarie.py
File metadata and controls
executable file
·127 lines (106 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python
import os
#define constants
pOccupied = 'O'
pFree = 'F'
pProhibited = 'P'
mGame = [
['P', 'P', 'O', 'O', 'O', 'P', 'P'],
['P', 'P', 'O', 'O', 'O', 'P', 'P'],
['O', 'O', 'O', 'O', 'O', 'O', 'O'],
['O', 'O', 'O', 'F', 'O', 'O', 'O'],
['O', 'O', 'O', 'O', 'O', 'O', 'O'],
['P', 'P', 'O', 'O', 'O', 'P', 'P'],
['P', 'P', 'O', 'O', 'O', 'P', 'P']]
#Cleaning function
class cls(object):
def __repr__(self):
os.system('cls' if os.name == 'nt' else 'clear')
return ''
#This function simply print the (in) Matrix
def printBoard(mBoard = []):
numRow = 0
print ' 0123456'
for rows in mBoard:
board = ''
for item in rows:
if item == 'P': board += ' '
elif item == 'O': board += 'X'
elif item == 'F': board += ' '
print str(numRow) + ' ' + board
numRow += 1
#this function consolidate the board with the correct move
def moveIt (posFrom = [], posTo = []):
posFromX = int(posFrom[0])
posFromY = int(posFrom[1])
posToX = int(posTo[0])
posToY = int(posTo[1])
mGame[posFromX][posFromY] = 'F'
mGame[(posFromX+posToX)//2][(posFromY+posToY)//2] = 'F'
mGame[posToX][posToY] = 'O'
#this function returns if a tab can move to the position
def canMove(posFrom = [], posTo = []):
posFromX = int(posFrom[0])
posFromY = int(posFrom[1])
posToX = int(posTo[0])
posToY = int(posTo[1])
mMoves = []
# Create a list with the 4 possible movements
# using the ternary operators (op1 if condition else op2)
if mGame[(posFromX+posToX)//2][(posFromY+posToY)//2] == 'O' :
mMoves.append([posFromX-2 , posFromY, mGame[posFromX - 2][posFromY]] if posFromX-2 >= 0 else 'P')
mMoves.append([posFromX , posFromY-2, mGame[posFromX][posFromY - 2]] if posFromY-2 >= 0 else 'P')
mMoves.append([posFromX+2 , posFromY, mGame[posFromX + 2][posFromY]] if posFromX+2 <= 5 else 'P')
mMoves.append([posFromX , posFromY+2, mGame[posFromX][posFromY + 2]] if posFromY+2 <= 5 else 'P')
if [posToX, posToY, pFree] in mMoves:
return True
else:
return False
def newGame ():
mGame = [['P', 'P', 'O', 'O', 'O', 'P', 'P'],
['P', 'P', 'O', 'O', 'O', 'P', 'P'],
['O', 'O', 'O', 'O', 'O', 'O', 'O'],
['O', 'O', 'O', 'F', 'O', 'O', 'O'],
['O', 'O', 'O', 'O', 'O', 'O', 'O'],
['P', 'P', 'O', 'O', 'O', 'P', 'P'],
['P', 'P', 'O', 'O', 'O', 'P', 'P']]
print 'Game Start'
newGame()
printBoard(mGame)
while 1 == 1:
selection = raw_input('Play>> ')
token = selection.split(' ')
if token[0] == 'move' and len(token) == 3 :
try:
porFrom = []
posFrom = token[1].split(',', 1)
posTo = []
posTo = token[2].split(',', 1)
if canMove(posFrom, posTo) :
moveIt(posFrom, posTo)
printBoard(mGame)
else :
print 'Movement not allowed!'
except ValueError:
print 'Bad Spelling!'
elif token[0] == 'print' :
printBoard(mGame)
elif token[0] == 'quit' :
print 'Bye'
break
elif token[0] == 'printBoard' :
print mGame
elif token[0] == 'help' :
print 'Commands:'
print ' move [POSITION SOURCE] [POSITION DESTINATION]'
print ' Example: move 1,3 3,3'
print ' print'
print ' printBoard (DEVELOPER COMMAND)'
print ' help'
print ' help'
print ' newGame'
elif token[0] == 'newGame' :
newGame()
printBoard(mGame)
else:
print 'Incorrect command'