forked from changgunyee/connect4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect4.py
90 lines (70 loc) · 2.65 KB
/
connect4.py
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
import numpy as np
from connect4_ai import *
ROW_SIZE = 6
COLUMN_SIZE = 7
board = np.full((ROW_SIZE, COLUMN_SIZE), -1)
column_index = [0 for num in range(COLUMN_SIZE)]
menu_str = "1 ~ 7번 중 컬럼 선택하세요."
"""AI는 0번, 사용자는 1번입니다."""
"""-1은 빈칸, 0은 AI, 1은 사용자입니다."""
def print_board():
for row in range(len(board)):
for column in range(len(board[ROW_SIZE - 1 - row])):
if board[ROW_SIZE - 1 - row][column] == 0:
print('|0', end='')
elif board[ROW_SIZE - 1 - row][column] == 1:
print('|X', end='')
else:
print('| ', end='')
print("|")
def choose(column, user=0):
column -= 1
if column_index[column] >= ROW_SIZE or column < 0 or column >= COLUMN_SIZE:
return False
board[column_index[column]][column] = user
column_index[column] += 1
print_board()
return True
def is_game_over(user):
for column in range(COLUMN_SIZE - 3):
for row in range(ROW_SIZE):
if board[row][column] == user and board[row][column + 1] == user and board[row][column + 2] == user and \
board[row][column + 3] == user:
return True
for column in range(COLUMN_SIZE):
for row in range(ROW_SIZE - 3):
if board[row][column] == user and board[row + 1][column] == user and board[row + 2][column] == user and \
board[row + 3][column] == user:
return True
for column in range(COLUMN_SIZE - 3):
for row in range(ROW_SIZE - 3):
if board[row][column] == user and board[row + 1][column + 1] == user and board[row + 2][
column + 2] == user and board[row + 3][column + 3] == user:
return True
for column in range(COLUMN_SIZE - 3):
for row in range(3, ROW_SIZE):
if board[row][column] == user and board[row - 1][column + 1] == user and board[row - 2][
column + 2] == user and board[row - 3][column + 3] == user:
return True
first_person = int(input("선공:1, 후공:2 선택하세요. :"))
if first_person == 1:
while True:
i = int(input(menu_str))
if choose(i, 1):
break
while True:
print("AI가 밑에 처럼 놓았습니다.")
while True:
choice_ai = ai(board)
if choose(choice_ai):
break
if is_game_over(0):
print("AI가 승리하였습니다.")
break
while True:
i = int(input(menu_str))
if choose(i, 1):
break
if is_game_over(1):
print("당신이 승리하였습니다.")
break