Last active
August 29, 2015 14:18
-
-
Save Zeirison/087fe629195a37029cf9 to your computer and use it in GitHub Desktop.
New functions and fixes
This file contains hidden or 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
| __author__ = 'Zeirison' | |
| import sys | |
| enemy = int(input("Please choose the opponent \n (1 - Human, 2 - Computer):")) | |
| field = ["_", "_", "_", "_", "_", "_", "_", "_", "_"] | |
| def section_check(a): | |
| return field[a - 1] == "_" | |
| def win_check(a): | |
| for i in range(0, 7, 3): | |
| if field[i] == a and field[i + 1] == a and field[i + 2] == a: | |
| return 1 | |
| for i in range(0, 3, 1): | |
| if field[i] == a and field[i + 3] == a and field[i + 6] == a: | |
| return 1 | |
| if field[0] == a and field[4] == a and field[8] == a: | |
| return 1 | |
| if field[2] == a and field[4] == a and field[6] == a: | |
| return 1 | |
| return 0 | |
| def status_check(a, b=""): | |
| return { | |
| 0: "Game proceeds.", | |
| 1: "The player '" + b + "' won!", | |
| 2: "The " + b + " won!", | |
| 3: "Draw!" | |
| }.get(a) | |
| def game(): | |
| global status | |
| hod = 0 | |
| opp = "Human" | |
| status = status_check(0) | |
| field.append(" --> Status: ") | |
| field.append(status) | |
| field.append(" --> Opponent: %s" % opp) | |
| for i in range(9): | |
| if hod == 0: | |
| zn = "X" | |
| hod = 1 | |
| else: | |
| zn = "0" | |
| hod = 0 | |
| check = False | |
| while not check: | |
| num = int(input("To put " + zn + " in a section: ")) | |
| check = section_check(num) | |
| if not check: | |
| print("This section is already occupied!") | |
| field[num - 1] = zn | |
| print(zn) | |
| if i == 8: | |
| status[10] = status_check(3, zn) | |
| else: | |
| field[10] = status_check(win_check(zn), zn) | |
| edit = "|{0}|{1}|{2}|{9}{10}" \ | |
| "\n|{3}|{4}|{5}|{11}" \ | |
| "\n|{6}|{7}|{8}|".format(*field) | |
| print(edit) | |
| if win_check(zn) == 1: | |
| break | |
| game() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment