Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- ####### HEAD #################
- n = int(input("ใส่ขนาดตาราง: "))
- table = [[0]*n for i in range(n)]
- turn = random.randint(1,2)
- r = 1
- c = 1
- ###########################################
- def reports():
- keep = "+"
- for i in range(n):
- keep+="---+"
- print(keep)
- for i in table:
- keep2 = "|"
- for j in i:
- if j == 0:
- keep2 = keep2 + " "+ "|"
- elif j == 1:
- keep2 = keep2 + " o "+ "|"
- else:
- keep2 = keep2 + " x "+ "|"
- print(keep2)
- print(keep)
- ############################################
- def setOthello():
- table[n//2-1][n//2-1] = 1
- table[n//2][n//2] = 1
- table[n//2][n//2-1] = 2
- table[n//2-1][n//2] = 2
- #############################################
- def getPoints():
- global turn
- global r
- global c
- show_1 = "ใส่ตำแหน่งที่ แถว 0-"+str(n-1)+": "
- show_2 = "ใส่ตำแหน่งที่ คอลัมน์ 0-"+str(n-1)+": "
- try:
- rows = int(input(show_1))
- cols = int(input(show_2))
- except:
- return True
- if table[rows][cols] !=0:
- return True
- else:
- table[rows][cols]=turn
- check(rows,cols,turn)
- if turn == 1:
- turn =2
- else:
- turn = 1
- return False
- ##########################################################################################################################################
- ######
- def upLeft(rows,cols,turn):
- global r
- global c
- if cols-c == -1 or rows-r == -1 or cols-c-1 == -1 or rows-r-1 == -1:
- return False
- if (table[rows-r][cols-c]!=0 and table[rows-r][cols-c]!=turn) and (rows-r != -1 and cols-c != -1):
- if table[rows-r-1][cols-c-1] == turn and (rows-r-1 != -1 and cols-c-1 != -1):
- table[rows-r][cols-c] = turn
- return True
- else:
- r+=1
- c+=1
- if upLeft(rows,cols,turn):
- r-=1
- c-=1
- table[rows-r][cols-c] = turn
- return True
- else:
- return False
- else:
- return False
- def upMid(rows,cols,turn):
- global r
- if rows-r == -1 or rows-r-1 == -1:
- return False
- if (table[rows-r][cols]!=0 and table[rows-r][cols]!=turn) and rows-r != -1 :
- if table[rows-r-1][cols] == turn and rows-r-1 != -1:
- table[rows-r][cols] = turn
- return True
- else:
- r+=1
- if upMid(rows,cols,turn):
- r-=1
- table[rows-r][cols] = turn
- return True
- else:
- return False
- else:
- return False
- def upRight(rows,cols,turn):
- global r
- global c
- if cols+c == n or rows-r == -1 or cols+c+1 == n or rows-r-1 == -1:
- return False
- if (table[rows-r][cols+c]!=0 and table[rows-r][cols+c]!=turn) and (rows-r != -1 and cols+c != n):
- if table[rows-r-1][cols+c+1] == turn and (rows-r-1 != -1 and cols+c+1 != n):
- table[rows-r][cols+c] = turn
- return True
- else:
- r+=1
- c+=1
- if upRight(rows,cols,turn):
- r-=1
- c-=1
- table[rows-r][cols+c] = turn
- return True
- else:
- return False
- else:
- return False
- def cenLeft(rows,cols,turn):
- global c
- if cols-c == -1 or cols-c-1 == -1:
- return False
- if (table[rows][cols-c]!=0 and table[rows][cols-c]!=turn) and cols-c != -1:
- if table[rows][cols-c-1] == turn and cols-c-1 != -1:
- table[rows][cols-c] = turn
- return True
- else:
- c+=1
- if cenLeft(rows,cols,turn):
- c-=1
- table[rows][cols-c] = turn
- return True
- else:
- return False
- else:
- return False
- def cenRight(rows,cols,turn):
- global c
- if cols+c == n or cols+c+1 == n:
- return False
- if (table[rows][cols+c]!=0 and table[rows][cols+c]!=turn) and cols+c != n:
- if table[rows][cols+c+1] == turn and cols+c+1 != n:
- table[rows][cols+c] = turn
- return True
- else:
- c+=1
- if cenRight(rows,cols,turn):
- c-=1
- table[rows][cols+c] = turn
- return True
- else:
- return False
- else:
- return False
- def downLeft(rows,cols,turn):
- global r
- global c
- if cols-c == -1 or rows+r == n or cols-c-1 == -1 or rows+r+1 == n:
- return False
- if (table[rows+r][cols-c]!=0 and table[rows+r][cols-c]!=turn) and (rows+r != n and cols-c != -1):
- if table[rows+r+1][cols-c-1] == turn and (rows+r+1 != n and cols-c-1 != -1):
- table[rows+r][cols-c] = turn
- return True
- else:
- r+=1
- c+=1
- if downLeft(rows,cols,turn):
- r-=1
- c-=1
- table[rows+r][cols-c] = turn
- return True
- else:
- return False
- else:
- return False
- def downMid(rows,cols,turn):
- global r
- if rows+r == n or rows+r+1 == n:
- return False
- if (table[rows+r][cols]!=0 and table[rows+r][cols]!=turn) and rows+r != n :
- if table[rows+r+1][cols] == turn and rows+r+1 != n:
- table[rows+r][cols] = turn
- return True
- else:
- r+=1
- if upMid(rows,cols,turn):
- r-=1
- table[rows+r][cols] = turn
- return True
- else:
- return False
- else:
- return False
- def downRight(rows,cols,turn):
- global r
- global c
- if cols+c == n or rows+r == n or cols+c+1 == n or rows+r+1 == n:
- return False
- if (table[rows+r][cols+c]!=0 and table[rows+r][cols+c]!=turn) and (rows+r != n and cols+c != n):
- if table[rows+r+1][cols+c+1] == turn and (rows+r+1 != n and cols+c+1 != n):
- table[rows+r][cols+c] = turn
- return True
- else:
- r+=1
- c+=1
- if downRight(rows,cols,turn):
- r-=1
- c-=1
- table[rows+r][cols+c] = turn
- return True
- else:
- return False
- else:
- return False
- #################################################################################################################################################
- def check(rows,cols,turn):
- global r
- global c
- r=1
- c=1
- upLeft(rows,cols,turn)
- r=1
- upMid(rows,cols,turn)
- r=1
- c=1
- upRight(rows,cols,turn)
- c=1
- cenLeft(rows,cols,turn)
- c=1
- cenRight(rows,cols,turn)
- r=1
- c=1
- downLeft(rows,cols,turn)
- r=1
- downMid(rows,cols,turn)
- r=1
- c=1
- downRight(rows,cols,turn)
- ##########################################################################################################################################
- def isWinner(turn):
- o = 0
- x = 0
- for i in range(n):
- for j in range(n):
- if table[i][j] == 1:
- o+=1
- elif table[i][j] == 2:
- x+=1
- else:
- return False
- if i == n-1 and j == n-1:
- if o>x:
- print("ผู้เล่นคน o Win")
- elif o==x:
- print("Draw")
- else:
- print("ผู้เล่นคน x Win")
- return True
- #########################################################################################################################################
- if __name__ == "__main__":
- setOthello()
- reports()
- while True:
- if turn == 1:
- print("\nผู้เล่นคน: o")
- else:
- print("\nผู้เล่นคน: x")
- if getPoints():
- continue
- reports()
- if isWinner(turn):
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement