Skip to main content

Tic Tac Toe

CODE IS BELOW:

Do Follow Our Instagram Page  & Share To Your Friends!







board = [' ' for x in range(10)] def insertLetter(letter, pos): board[pos] = letter def spaceIsFree(pos): return board[pos] == " " def printBoard(board): print(" " + board[1] + "| " + board[2] + "| " + board[3]) print("---------") print(" " + board[4] + "| " + board[5] + "| " + board[6]) print("---------") print(" " + board[7] + "| " + board[8] + "| " + board[9]) def isWinner(bo, le): return ((bo[7] == le and bo[8] == le and bo[9] == le) or (bo[4] == le and bo[5] == le and bo[6] == le) or (bo[1] == le and bo[2] == le and bo[3] == le) or (bo[1] == le and bo[4] == le and bo[7] == le) or (bo[2] == le and bo[5] == le and bo[8] == le) or (bo[3] == le and bo[6] == le and bo[9] == le) or (bo[1] == le and bo[5] == le and bo[9] == le) or (bo[3] == le and bo[5] == le and bo[7] == le)) def playMove(): run = True while run: move = input("Please select a position to place an 'X' (1-9): ") try: move = int(move) if move > 0 and move < 10 : if spaceIsFree(move): run = False insertLetter("x", move) else: print("Sorry this space is occupied") else: print("Please type the number within the range") except: print("Please type a number. ") def compMove(): possibleMoves = [x for x, letter in enumerate(board) if letter == ' ' and x != 0] move = 0 for let in ['o','x']: for i in possibleMoves: boardCopy = board[:] boardCopy[i] = let if isWinner(boardCopy, let): move = i return move cornersOpen = [] for i in possibleMoves: if i in [1,3,7,9]: cornersOpen.append(i) if len(cornersOpen) > 0: move = selectRandom(cornersOpen) return move if 5 in possibleMoves: move = 5 return move edgesOpen = [] for i in possibleMoves: if i in [2,4,6,8]: edgesOpen.append(i) if len(edgesOpen) > 0: move = selectRandom(edgesOpen) return move def selectRandom(li): import random ln = len(li) r = random.randrange(0, ln) return li[r] def isBoardFull(board): if board.count(" ") > 1: return False else: return True def main(): print("welcome tic tac toe") printBoard(board) while not(isBoardFull(board)): if not(isWinner(board, "o")): playMove() printBoard(board) else: print("Sorry, O's won this time! ") break if not(isWinner(board, "x")): move = compMove() if move == 0: print("Tie game") else: insertLetter("o", move) print("Computer placed an 'o' in position", move, ":") printBoard(board) else: print("X's won this time! Good job") break if isBoardFull(board): print("Tie game") main()

Comments

Popular posts from this blog

Drawing A Design In Python

 Code :  from turtle import *  from random import randint  bgcolor('black')  x = 1  speed(0)  while x < 400:      r = randint(0,255)   g = randint(0,255)    b = randint(0,255)      colormode(255)    pencolor(r,g,b)   fd(50 + x)   rt(90.991)   x = x+1     exitonclick() Result : 

PIKACHU

CODE FOR DRAWING THIS PIKACHU IS BELOW: import turtle def getPosition(x, y): turtle.setx(x) turtle.sety(y) print(x, y) class Pikachu: def __init__(self): self.t = turtle.Turtle() t = self.t t.pensize(3) t.speed(9) t.ondrag(getPosition) def noTrace_goto(self, x, y): self.t.penup() self.t.goto(x, y) self.t.pendown() def leftEye(self, x, y): self.noTrace_goto(x, y) t = self.t t.seth(0) t.fillcolor('#333333') t.begin_fill() t.circle(22) t.end_fill() self.noTrace_goto(x, y+10) t.fillcolor('#000000') t.begin_fill() t.circle(10) t.end_fill() self.noTrace_goto(x+6, y + 22) t.fillcolor('#ffffff') t.begin_fill() t.circle(10) t.end_fill() def rightEye(self, x, y): self.noTrace_goto(x, y) t = self.t ...

Drawing Doraemon

CODE:  from turtle import * def my_goto(x, y): penup() goto(x, y) pendown() def eyes(): fillcolor("#ffffff") begin_fill() tracer(False) a = 2.5 for i in range(120): if 0 <= i < 30 or 60 <= i < 90: a -= 0.05 lt(3) fd(a) else: a += 0.05 lt(3) fd(a) tracer(True) end_fill() def beard(): my_goto(-32, 135) seth(165) fd(60) my_goto(-32, 125) seth(180) fd(60) my_goto(-32, 115) seth(193) fd(60) my_goto(37, 135) seth(15) fd(60) my_goto(37, 125) seth(0) fd(60) my_goto(37, 115) seth(-13) fd(60) def mouth(): my_goto(5, 148) seth(270) fd(100) seth(0) circle(120, 50) seth(230) circle(-120, 100) def scarf(): fillcolor('#e70010') begin_fill() seth(0) fd(200) circle(-5, 90) fd(10) circle(-5, 90) fd(20...