-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit de2f7ff
Showing
15 changed files
with
1,970 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Auto detect text files and perform LF normalization | ||
* text=auto |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
#!/usr/bin/env python3 | ||
""" | ||
check the security and functionability of uploaded code | ||
- forbid from importing os | ||
- random chessboard check | ||
- some special case check | ||
""" | ||
import imp | ||
import traceback | ||
import sys | ||
import os | ||
import numpy as np | ||
|
||
|
||
FORBIDDEN_LIST = ['import os', 'exec'] | ||
|
||
class CodeCheck(): | ||
def __init__ (self, script_file_path, chessboard_size): | ||
self.time_out = 5 | ||
self.script_file_path = script_file_path | ||
self.chessboard_size = chessboard_size | ||
self.agent = None | ||
self.errormsg = 'Error' | ||
# sys.stdout = open(os.devnull, 'w') | ||
# sys.stderr = open(os.devnull, 'w') | ||
# print(self.chessboard) | ||
|
||
# Call this function and get True or False, self.errormsg has the massage | ||
def check_code(self): | ||
# check if contains forbidden library | ||
if self.__check_forbidden_import() == False: | ||
return False | ||
|
||
# check initialization | ||
try: | ||
self.agent = imp.load_source('AI', self.script_file_path).AI(self.chessboard_size, 1, self.time_out) | ||
self.agent = imp.load_source('AI', self.script_file_path).AI(self.chessboard_size, -1, self.time_out) | ||
except Exception: | ||
self.errormsg = "Fail to init" | ||
return False | ||
|
||
# check simple condition | ||
if not self.__check_simple_chessboard(): | ||
self.errormsg = "Can not pass usability test." | ||
return False | ||
|
||
# check advance condition, online test contain more test case than this demo | ||
if not self.__check_advance_chessboard(): | ||
self.errormsg = "Your code is too weak, fail to pass base test." | ||
return False | ||
|
||
return True | ||
|
||
|
||
def __check_forbidden_import(self): | ||
with open(self.script_file_path, 'r', encoding='UTF-8') as myfile: | ||
data = myfile.read() | ||
for keyword in FORBIDDEN_LIST: | ||
idx = data.find(keyword) | ||
if idx != -1: | ||
self.errormsg = "import forbidden" | ||
return False | ||
return True | ||
|
||
def __check_go (self, chessboard): | ||
self.agent = imp.load_source('AI', self.script_file_path).AI(self.chessboard_size, -1, self.time_out) | ||
try: | ||
self.agent.go(np.copy(chessboard)) | ||
except Exception: | ||
self.errormsg = "Error:" + traceback.format_exc() | ||
print(self.errormsg) | ||
return False | ||
return True | ||
|
||
def __check_result (self, chessboard, result): | ||
if not self.__check_go(chessboard): | ||
return False | ||
print(chessboard) | ||
print(self.agent.candidate_list[-1]) | ||
if not self.agent.candidate_list or list(self.agent.candidate_list[-1]) not in result: | ||
return False | ||
return True | ||
|
||
def __check_simple_chessboard(self): | ||
# empty chessboard | ||
if not self.__check_go(np.zeros((self.chessboard_size, self.chessboard_size), dtype=np.int)): | ||
return False | ||
|
||
# only one empty position remain | ||
chessboard = np.ones((self.chessboard_size, self.chessboard_size)) | ||
chessboard[:, ::2] = -1 | ||
for i in range(0, self.chessboard_size, 4): | ||
chessboard[i] = -chessboard[i] | ||
x, y = np.random.choice(self.chessboard_size, 2) | ||
chessboard[x, y] = 0 | ||
|
||
if not self.__check_result(chessboard, [[x, y]]): | ||
return False | ||
|
||
return True | ||
|
||
def __check_advance_chessboard (self): | ||
# win | ||
chessboard = np.zeros((self.chessboard_size, self.chessboard_size), dtype=np.int) | ||
chessboard[0, 0:4] = -1 | ||
chessboard[1, 0:4] = 1 | ||
if not self.__check_result(chessboard, [[0, 4]]): | ||
return False | ||
# 斜赢 | ||
chessboard = np.zeros((self.chessboard_size, self.chessboard_size), dtype=np.int) | ||
chessboard[0, 4:6] = 1 | ||
chessboard[0, 7] = 1 | ||
chessboard[1, 3] = -1 | ||
chessboard[2, 4] = -1 | ||
chessboard[3, 5] = -1 | ||
chessboard[4, 6] = -1 | ||
if not self.__check_result(chessboard, [[0, 2], [5, 7]]): | ||
return False | ||
|
||
# defense 5 inline | ||
chessboard = np.zeros((self.chessboard_size, self.chessboard_size), dtype=np.int) | ||
chessboard[0, 0:2] = -1 | ||
chessboard[0, 7] = -1 | ||
chessboard[1, 1:4] = 1 | ||
if not self.__check_result(chessboard, [[1, 4], [1, 0], [1, 5]]): | ||
return False | ||
# 斜defense 5 | ||
chessboard = np.zeros((self.chessboard_size, self.chessboard_size), dtype=np.int) | ||
chessboard[2:5, 8] = -1 | ||
chessboard[1, 1] = -1 | ||
chessboard[2, 2] = 1 | ||
chessboard[3, 3] = 1 | ||
chessboard[4, 4] = 1 | ||
chessboard[5, 5] = 1 | ||
if not self.__check_result(chessboard, [[6, 6]]): | ||
return False | ||
|
||
# two three | ||
chessboard = np.zeros((self.chessboard_size, self.chessboard_size), dtype=np.int) | ||
chessboard[1, 1:3] = -1 | ||
chessboard[2:4, 3] = -1 | ||
chessboard[1, 6:8] = 1 | ||
chessboard[2:4, 8] = 1 | ||
if not self.__check_result(chessboard, [[1, 3]]): | ||
return False | ||
# 斜攻双三 | ||
chessboard = np.zeros((self.chessboard_size, self.chessboard_size), dtype=np.int) | ||
chessboard[0, 0:2] = 1 | ||
chessboard[0:2, self.chessboard_size - 1] = 1 | ||
chessboard[1, 6] = -1 | ||
chessboard[2, 7] = -1 | ||
chessboard[4, 7] = -1 | ||
chessboard[5, 6] = -1 | ||
if not self.__check_result(chessboard, [[3, 8]]): | ||
return False | ||
chessboard = np.zeros((self.chessboard_size, self.chessboard_size), dtype=np.int) | ||
chessboard[0, 0:2] = 1 | ||
chessboard[0:2, self.chessboard_size - 1] = 1 | ||
chessboard[1, 6] = -1 | ||
chessboard[2, 5] = -1 | ||
chessboard[4, 5] = -1 | ||
chessboard[5, 6] = -1 | ||
if not self.__check_result(chessboard, [[3, 4]]): | ||
return False | ||
# defense | ||
chessboard = np.zeros((self.chessboard_size, self.chessboard_size), dtype=np.int) | ||
chessboard[0, 0:2] = -1 | ||
chessboard[0:2, self.chessboard_size - 1] = -1 | ||
chessboard[1, 6:8] = 1 | ||
chessboard[2:4, 8] = 1 | ||
if not self.__check_result(chessboard, [[0, 8], [1, 8], [4, 8], [5, 8], [1, 5], [1, 9], [1, 10]]): | ||
return False | ||
# 斜防双三 | ||
chessboard = np.zeros((self.chessboard_size, self.chessboard_size), dtype=np.int) | ||
chessboard[0, 0:2] = -1 | ||
chessboard[0:2, self.chessboard_size - 1] = -1 | ||
chessboard[1, 6] = 1 | ||
chessboard[2, 7] = 1 | ||
chessboard[4, 7] = 1 | ||
chessboard[5, 6] = 1 | ||
if not self.__check_result(chessboard, [[3, 8], [0, 5], [6, 5]]): | ||
return False | ||
chessboard = np.zeros((self.chessboard_size, self.chessboard_size), dtype=np.int) | ||
chessboard[0, 0:2] = -1 | ||
chessboard[0:2, self.chessboard_size - 1] = -1 | ||
chessboard[1, 6] = 1 | ||
chessboard[2, 5] = 1 | ||
chessboard[4, 5] = 1 | ||
chessboard[5, 6] = 1 | ||
if not self.__check_result(chessboard, [[3, 4], [0, 7], [6, 7]]): | ||
return False | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/usr/bin/env python3 | ||
import sys | ||
from code_check import CodeCheck | ||
def main(): | ||
code_checker = CodeCheck('D:\PycharmProject\Gobang\main1.py', 15) | ||
if not code_checker.check_code(): | ||
print(code_checker.errormsg) | ||
else: | ||
print('pass') | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import numpy as np | ||
from main1 import AI as AI | ||
|
||
|
||
def display_chessboard(chessboard): | ||
colors = ['·', '◯', '◉'] | ||
print('\t', end='') | ||
for i in range(chessboard[0].size): | ||
print(i, end='\t') | ||
print() | ||
start = 0 | ||
for e in chessboard: | ||
print(start, end=':\t') | ||
for j in e: | ||
print(colors[j], end='\t') | ||
start += 1 | ||
print() | ||
|
||
|
||
# -1 black, 1 white | ||
def play(chessboard, chess, inChess): | ||
x, y = inChess | ||
if x < 0 or x > 14 or y < 0 or y > 14: | ||
print('越界了重新下') | ||
play(chessboard, chess) | ||
return | ||
if chessboard[x][y] != 0: | ||
print('当前位置已经满了,请重新下') | ||
play(chessboard, chess) | ||
return | ||
if chess == -1: | ||
chessboard[x][y] = -1 | ||
if chess == 1: | ||
chessboard[x][y] = 1 | ||
|
||
|
||
if __name__ == '__main__': | ||
AI_COLOR = 1 | ||
chessboard = np.zeros([15, 15], dtype=int) | ||
# start with black chess | ||
chessNow = -1 | ||
# AI hold while chess | ||
A = AI(15, AI_COLOR, 15) | ||
size = A.chessboard_size | ||
player_step = 0 | ||
while True: | ||
if chessNow == AI_COLOR: | ||
A.go(chessboard) | ||
AI_result = A.get_result() | ||
play(chessboard, chessNow, AI_result) | ||
else: | ||
x, y = input().split() | ||
x, y = int(x), int(y) | ||
User_result = (x, y) | ||
print('对方下在了',User_result) | ||
play(chessboard, chessNow, User_result) | ||
# print(evalocal(size, User_result, chessboard, 1)) | ||
display_chessboard(chessboard) | ||
chessNow = - chessNow |
Oops, something went wrong.