Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 2011/12/15 - Finished the final version of the Kalah Gameboard
- # - Started the commencing function
- # 2011/12/16 - Finished commenced function at home
- # - Half completed houseEmpty and numSeedsInHouse function
- # 2011/12/19
- # - Finished houseEmpty
- # - FinishednumSeedsInHouse functions
- # - Fixed up main program
- # 2011/12/20
- # - Planning how to do sowSeeds function
- # 2011/12/21
- # - Trying to complete sowSeeds function
- # 2011/12/28
- # - Completed sowSeeds function and fixed up houseEmpty function
- # 2011/12/29
- # - Error checking done on all finished work
- # 2012/1/9
- # - Fixed up main program again to accomadate new work
- # - canPlay function started
- # - gameLoop function created and finished
- # 2012/1/10
- # - CanPlay function almost finished
- # - anotherTurn function created and almost finished
- # 2012/1/11
- # - canPlay and anotherTurn function finished
- # - Error checking done for all functions to run smoothly
- # - gameOver function created and finished.
- # 2012/1/12
- # - canCapture function completed. Fixing errors and error checking
- #Gameboard layout which shows the user the gameboard every turn.
- def drawGameboard(L):
- print()
- print(" _ _ _ _ _ _ _ _ _ _ _ _ ")
- print("| | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ | |")
- print("| || || || || || || || |")
- print("| || {:2d} || {:2d} || {:2d} || {:2d} || {:2d} || {:2d} || |".format(L[12], L[11], L[10], L[9], L[8], L[7]))
- print("| ||_ _ _ _||_ _ _ _||_ _ _ _||_ _ _ _||_ _ _ _||_ _ _ _|| |")
- print("| {:2d} | 13 12 11 10 9 8 | {:2d} |" .format(L[13], L[6]))
- print("| | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ | |")
- print("| || || || || || || || |")
- print("| || {:2d} || {:2d} || {:2d} || {:2d} || {:2d} || {:2d} || |".format(L[0], L[1], L[2], L[3], L[4], L[5]))
- print("| ||_ _ _ _||_ _ _ _||_ _ _ _||_ _ _ _||_ _ _ _||_ _ _ _|| |")
- print("|_ _ _ _ _ _| 1 2 3 4 5 6 |_ _ _ _ _ _|")
- print(" 14 7")
- #Function which gets the number of seeds in each house from the user
- def Commence():
- print("--------------------------Game Commenced--------------------")
- print()
- print("--------------------Creating Beginning Gameboard--------------------")
- print()
- #Gets number of seeds from user
- num = int(input(" Enter how many stones are in each house from 3-6: "))
- while num < 3 or num >6:
- print("Invalid input.")
- print()
- num = int(input("Please enter again how many stones are in each house?: "))
- print()
- print()
- print()
- print(" ", num, "stones have been added to each house")
- #Puts the amount of seeds entered by the user into a list
- gameNums = [num,num,num,num,num,num,0,num,num,num,num,num,num,0]
- #Returns house number
- return gameNums
- #Function which finds the amount of seeds in the house
- def numSeedsInHouse(L,houseNum):
- #Returns amount of seeds in the house
- return L[houseNum]
- #Function which asks user which house to empty, depending on the player
- def houseEmpty(player):
- print()
- #Asks user which house to empty
- playerShouse = input("Enter which house you want to empty player {0}: ".format(player))
- if player == 1:
- #Uses a loop until user types in the right input
- while playerShouse.isdigit() == False or int(playerShouse) < 0 or int(playerShouse) >6:
- print()
- print("Invalid input.")
- print()
- playerShouse = input("Please enter again which house you want to empty player 1: ")
- #Returns house number that player 1 wants to empty
- return int(playerShouse)-1
- if player == 2:
- #Uses a loop until user types in the right input
- while playerShouse.isdigit() == False or int(playerShouse) < 8 or int(playerShouse) >13:
- print()
- print("Invalid input.")
- print()
- playerShouse = input("Please enter again which house you want to empty player 2: ")
- #Returns house number that player 2 wants to empty
- return int(playerShouse)-1
- #Function which sees if the player has seeds in their houses to play
- def canPlay(L,player):
- if player == 1:
- #Checks houses 0-6
- for i in range(0,6):
- #Checks each index from 0-6, and if one of the numbers do not equal 0, returns True
- if L[i] != 0:
- return True
- #Returns false if all of them are 0
- return False
- if player == 2:
- #Checks houses 8-13
- for i in range(8,14):
- #Checks each index from 8-13, and if one of the numbers do not equal 0, returns True
- if L[i] != 0:
- return True
- #Returns false if all of them are 0
- return False
- #Function which checks
- def canCapture(houseNum,numSeeds,L,player):
- #numSeeds = the number of seeds in the chosen house
- numSeeds = numSeedsInHouse(L,houseNum)
- # temp is equal to the list index
- temp = L[houseNum]
- # Changes the list index to zero
- L[houseNum] = 0
- if player == 1:
- #range of seeds is from 0 to 6
- rangeSeeds = range(0,7)
- #capList is the capture list. Every 2 numbers are paired, so house 0 and 12 are across from each other, house 1 and 11 are across from each other, etc.
- capList = [0,12,1,11,2,10,3,9,4,8,5,7]
- kalahNum = 6
- anotherTurn = 2
- Pnum = 13
- if player == 2:
- # Range of seeds from 8 to 14
- rangeSeeds = range(8,14)
- # Caplist for player 2
- capList = [7,5,8,4,9,3,10,2,11,1,12,0]
- #Kalah number
- kalahNum = 13
- anotherTurn = 1
- Pnum = 6
- #
- for y in range(numSeeds):
- if houseNum+numSeeds > 13:
- houseNum = -1 - (14-houseNum-1)
- if L[houseNum+numSeeds] == 0 and houseNum+numSeeds+1 in rangeSeeds:
- print ("Captured!")
- for x in range(1,numSeeds):
- if x+houseNum > 13:
- housenum = -x
- L[houseNum+x] += 1
- L[kalahNum] += 1 + L[capList[capList.index(houseNum+numSeeds)+1]]
- L[houseNum] = 0
- L[capList[capList.index(houseNum+numSeeds)+1]] = 0
- print("You get another turn!")
- gameLoop(L,anotherTurn)
- else:
- for iterateHouse in range(1,temp+1):
- if houseNum+iterateHouse == Pnum:
- L[houseNum+iterateHouse] = L[houseNum+iterateHouse]
- else:
- L[houseNum+iterateHouse] += 1
- return True
- return False
- def sowSeeds():
- while player == 1:
- temp = L[w]
- L[w] = 0
- numberofseeds = 0
- while temp!=0:
- L[w+temp] += 1
- temp-=1
- numberofseeds +=1
- return numberofseeds
- while player == 2:
- temp = L[w]
- L[w] = 0
- numberofseeds = 0
- while temp!=0:
- L[w+temp] += 1
- temp-=1
- numberofseeds +=1
- return numberofseeds
- #Function which checks if player 1 or 2 can get another turn
- def anotherTurn(numberofseeds,houseNum,player,L):
- if player == 1:
- if numberofseeds + houseNum+1 == 7:
- print("You get another turn!")
- #Reruns gameLoop to get another turn
- gameLoop(L,2)
- if player == 2:
- if numberofseeds + houseNum+1 == 14:
- print("You get another turn!")
- #Reruns gameLoop to get another turn
- gameLoop(L,1)
- #Function which checks if the game is over
- def gameOver(L):
- check=0
- for x in L[:6]:
- check+=x
- if check==0:
- print ("Game over. Player 1 has 0 seeds")
- for x in range(7,13):
- L[13]+=L[x]
- L[x]=0
- print ("Player two gets remaining seeds. \nPlayer two now has {0} seeds in Kalah".format(L[13]))
- print ("Player one has {0} seeds in Kalah".format(L[6]))
- if L[6] > L[13]:
- print ("Player one wins game!")
- elif L[13] > L[6]:
- print ("Player two wins game!")
- else:
- print ("Draw game.")
- drawGameboard(L)
- check=0
- for x in L[7:13]:
- check+=x
- if check == 0:
- print ("Game over. Player 2 has 0 seeds")
- for x in range(0,6):
- L[6]+=L[x]
- L[x]=0
- print ("Player one gets remaining seeds. \nPlayer one now has {0} seeds in Kalah".format(L[6]))
- print ("Player two has {0} seeds in Kalah".format(L[13]))
- if L[6] > L[13]:
- print ("Player one wins game!")
- elif L[13] > L[6]:
- print ("Player two wins game!")
- else:
- print ("Draw game.")
- drawGameboard(L)
- print ("Continue")
- return False
- #Function which loops the game with the following functions to switch turns between players
- def gameLoop(L,player):
- while gameOver(L)!= True:
- if player == 1:
- player = 2
- else:
- player = 1
- drawGameboard(L)
- houseNum = houseEmpty(player)
- numseeds = numSeedsInHouse(L,houseNum)
- checkPlaying = canPlay(L,player)
- canCapture(houseNum,numseeds,L,player)
- anotherTurn(numseeds,houseNum,player,L)
- def main():
- player = 2
- L = Commence()
- print(" ", "Kalah GameBoard has been created!")
- gameLoop(L,player)
- # Main Program
- main()
Add Comment
Please, Sign In to add comment