import
random
import
math
from
cards
import
Deck
from
cards
import
Card
def
setup():
global
gametime
gametime
=
0
d
=
Deck()
d.shuffle()
foundation
=
[[], [], [], []]
tableau1
=
[]
tableau2
=
[]
tableau3
=
[]
tableau4
=
[]
tableau5
=
[]
tableau6
=
[]
tableau7
=
[]
tableau8
=
[]
global
cell
cell
=
[(),(),(),()]
for
i
in
range
(
0
,
7
):
tableau1.append(d.deal())
for
i
in
range
(
7
,
14
):
tableau2.append(d.deal())
for
i
in
range
(
14
,
21
):
tableau3.append(d.deal())
for
i
in
range
(
21
,
28
):
tableau4.append(d.deal())
for
i
in
range
(
28
,
34
):
tableau5.append(d.deal())
for
i
in
range
(
34
,
40
):
tableau6.append(d.deal())
for
i
in
range
(
40
,
46
):
tableau7.append(d.deal())
for
i
in
range
(
46
,
52
):
tableau8.append(d.deal())
tableau
=
[tableau1, tableau2, tableau3, tableau4, tableau5, tableau6, tableau7, tableau8]
return
foundation, tableau, cell
def
move_to_foundation(tableau, foundation, t_col, f_col):
one
=
tableau[t_col][
-
1
]
two
=
foundation[f_col]
if
foundation[f_col]
=
=
[]:
if
one.get_rank()
=
=
1
:
foundation[f_col].append(one)
del
tableau[t_col][
-
1
]
elif
one.get_suit()
=
=
two.get_suit():
if
one.get_rank()
=
=
two.get_rank()
+
1
:
foundation[f_col].append(one)
del
tableau[t_col][
-
1
]
def
move_to_cell(tableau, cell, t_col, c_col):
one
=
tableau[t_col][
-
1
]
two
=
cell[c_col]
if
two
=
=
[]:
cell[c_col].append(one)
del
tableau[t_col][
-
1
]
def
move_to_tableau(tableau, foundation, t_col, f_col, c_col):
one
=
tableau[t_col][
-
1
]
if
cell[c_col] !
=
[]:
two
=
cell[c_col][
0
]
if
one
=
=
two
+
1
:
if
"d"
in
one
or
"h"
in
one:
if
"s"
in
two
or
"c"
in
two:
tableau[t_col].append(two)
del
cell[c_col][
-
1
]
elif
"d"
in
two
or
"h"
in
two:
if
"s"
in
one
or
"c"
in
one:
tableau[t_col].append(two)
del
cell[c_col][
-
1
]
def
is_winner(foundation):
gametime
=
0
while
gametime
=
=
0
:
if
foundation[
0
] !
=
[]:
if
foundation[
1
] !
=
[]:
if
foundation[
2
] !
=
[]:
if
foundation[
3
] !
=
[]:
print
(
"You won!"
)
gametime
=
1
def
move_in_tableau(tableau, t_col_source, t_col_dest):
one
=
tableau[t_col_source][
-
1
]
two
=
tableau[t_col_dest][
-
1
]
if
one
=
=
two
+
1
:
if
"d"
in
one
or
"h"
in
one:
if
"s"
in
two
or
"c"
in
two:
tableau[t_col_dest].append(one)
del
cell[t_col_source][
-
1
]
elif
"d"
in
two
or
"h"
in
two:
if
"s"
in
one
or
"c"
in
one:
tableau[t_col_dest].append(one)
del
cell[t_col_source][
-
1
]
pass
def
print_game(foundation, tableau, cell):
print
()
print
(
" Cells: Foundation:"
)
for
i
in
range
(
4
):
print
(
'{:8d}'
.
format
(i
+
1
), end
=
'')
print
(
' '
, end
=
'')
for
i
in
range
(
4
):
print
(
'{:8d}'
.
format
(i
+
1
), end
=
'')
print
()
for
c
in
cell:
try
:
print
(
'{:8s}'
.
format
(c[
0
].rank_and_suit()), end
=
'')
except
IndexError:
print
(
'{:8s}'
.
format
('
'), end='
')
print
(
' '
, end
=
'')
for
stack
in
foundation:
try
:
print
(
'{:8s}'
.
format
(stack[
-
1
].rank_and_suit()), end
=
'')
except
IndexError:
print
(
'{:8s}'
.
format
('
'), end='
')
print
()
print
(
'----------'
)
print
(
"Tableau"
)
for
i
in
range
(
len
(tableau)):
print
(
'{:8d}'
.
format
(i
+
1
), end
=
'')
print
()
max_length
=
max
([
len
(stack)
for
stack
in
tableau])
for
i
in
range
(max_length):
print
(
' '
*
7
, end
=
'')
for
stack
in
tableau:
try
:
print
(
'{:8s}'
.
format
(stack[i].rank_and_suit()), end
=
'')
except
IndexError:
print
(
'{:8s}'
.
format
('
'), end='
')
print
()
print
(
'----------'
)
def
print_rules():
print
(
"Rules of FreeCell"
)
print
(
"Goal"
)
print
(
"\tMove all the cards to the Foundations"
)
print
(
"Foundation"
)
print
(
"\tBuilt up by rank and by suit from Ace to King"
)
print
(
"Tableau"
)
print
(
"\tBuilt down by rank and by alternating color"
)
print
(
"\tThe bottom card of any column may be moved"
)
print
(
"\tAn empty spot may be filled with any card "
)
print
(
"Cell"
)
print
(
"\tCan only contain 1 card"
)
print
(
"\tThe card may be moved"
)
def
show_help():
print
(
"Responses are: "
)
print
(
"\t t2f #T #F - move from Tableau to Foundation"
)
print
(
"\t t2t #T1 #T2 - move card from one Tableau column to another"
)
print
(
"\t t2c #T #C - move from Tableau to Cell"
)
print
(
"\t c2t #C #T - move from Cell to Tableau"
)
print
(
"\t c2f #C #F - move from Cell to Foundation"
)
print
(
"\t 'h' for help"
)
print
(
"\t 'q' to quit"
)
def
play():
print_rules()
foundation, tableau, cell
=
setup()
show_help()
while
True
:
print_game(foundation, tableau, cell)
response
=
input
(
"Command (type 'h' for help): "
)
response
=
response.strip()
response_list
=
response.split()
if
len
(response_list) >
0
:
r
=
response_list[
0
]
f_col
=
response_list[
2
]
t_col
=
response_list[
1
]
c_col
=
response_list[
2
]
if
r
=
=
't2f'
:
move_to_foundation(tableau, foundation, t_col, f_col)
elif
r
=
=
't2t'
:
move_in_tableau(tableau, t_col_source, t_col_dest)
elif
r
=
=
't2c'
:
move_to_cell(tableau, cell, t_col, c_col)
elif
r
=
=
'c2t'
:
move_to_tableau(tableau, cell, t_col, c_col)
elif
r
=
=
'c2f'
:
move_to_foundation(tableau, foundation, t_col, f_col)
elif
r
=
=
'q'
:
break
elif
r
=
=
'h'
:
show_help()
else
:
print
(
'Unknown command:'
, r)
else
:
print
(
"Unknown Command:"
, response)
print
(
'Thanks for playing'
)
play()