From a86f855730956cc384d2169846afe9e45e36c08d Mon Sep 17 00:00:00 2001 From: Jack-Benny Persson Date: Tue, 13 Oct 2015 04:09:50 +0200 Subject: [PATCH] Second commit, added files --- README.md | 8 ++++++++ tictac.py | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 README.md create mode 100755 tictac.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..bec7972 --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ +# Tic Tac Toe # +This is just a simple Tic Tac Toe game I made during a Python class. Hopefully +it can be of use to someone. There are many many ways to write Tic Tac Toe +games, this is just one of many versions I did. This is the one the least amount +of code. + +## License ## +This program is made by Jack-Benny Persson is release under GNU GPL version 2. diff --git a/tictac.py b/tictac.py new file mode 100755 index 0000000..9a418a8 --- /dev/null +++ b/tictac.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +import sys + +gameplan = range(1, 10, 1) + +def print_gameplan(): + global gameplan + count = 0 + for i in gameplan: + print i, " ", + count += 1 + if count == 3: + print "\n" + count = 0 + +def check_win(): + global gameplan + win_cond = ((0, 1, 2), (3, 4, 5), (6, 7, 8), (0, 3, 6), (1, 4, 7), + (2, 5, 8), (0, 4, 8), (2, 4, 6)) + players = ["X", "O"] + for p in players: + for i in win_cond: + count = 0 + for j in i: + if gameplan[j] == p: + count += 1 + if count == 3: + print p, "wins!!!" + sys.exit(0) + +def main(): + global gameplan + print_gameplan() + + turn = 1 + round_check = 0 + while True: + if turn == 1: + player = "X" + turn += 1 + elif turn == 2: + player = "O" + turn -= 1 + print "Now it's " + player + " turn" + while True: + move = input("Make your move: ") + move -= 1 + if move <= 8 and move >= 0: + if isinstance(gameplan[move], int): + gameplan[move] = player + break + print_gameplan() + check_win() + round_check += 1 + if round_check == 9: + print "It's a draw!" + sys.exit(0) + +if __name__=='__main__': + main()