# Created by Vish Gaur and Atharva Gorantiwar
# Final version updated October 13, 2020
# This program is called Turtle Race. It is a 2 player game where each player stragtegicaly chooses turtles
# to try and score as efficiently as possible. It also has visual elements and can always be updated in the
# future to include more and more features - it has unlimited potential!

# importing necessary packages 
import turtle as trtl
import random
# instance of the turtle that will be used to set up the game 
painter = trtl.Turtle()

# instructions for how to play will be displayed. It is lengthy but necessary to play.
print ("How to play: Decide with your friend who wants to be player 1 and player 2. Red, you are player 1, Blue, you are player 2.")
print ("When it's your turn, a die will be rolled. You will then have to select which figure you want to move by the amount generated by the die. Counting from top to bottom, enter a number between 1 and 5 (respectivly) for which figure you want to move. In each row, the first player to get their figure to the middle line will score a point.")
print ("You will each play once every round. You will know a round has concluded as the game will remind you of your score. In each round Player 1 (Red) will be going first.")
print ("Go ahead, Have Fun!")

# goes to top right corner of game board
painter.speed(0)
painter.penup()
painter.goto(225,150)
painter.setheading(-180)
painter.pendown()

# This loop will draw the main board rectangle 
x = 0
while (x < 2):
  painter.forward(450)
  painter.left(90)
  painter.forward(300)
  painter.left(90)
  x += 1

# This loop will draw vertical lines that represent how far the turtle will move on each turn
# They are supposed to mimic the yard lines on a football field and will give the player an idea of 
# how far they need to move to score visually.
x = 0 
xinitial = -225
while (x < 11):
    xinitial += 37.501
    painter.penup()
    painter.goto(xinitial, 150)
    painter.setheading(270)
    painter.pendown()
    painter.forward(300)
    x += 1

# This block adds more visuals to the game board. It adds a triangle to craete the "house"
# of turtles and adds a King Turtle that shows the winning line.
painter.penup()
painter.goto(-225,150)
painter.pendown()
painter.goto(0,250)
painter.goto(225,150)
painter.penup()
painter.goto(0,175)
painter.fillcolor("gold")
painter.turtlesize(5)
painter.stamp()
painter.turtlesize(1)
painter.color("gold")
painter.pendown()
painter.forward(325)

# hides set up turtle because its work is done 
painter.hideturtle()

# initializes main variables and defines lists 
p1score = 0
p2score = 0
player1_turtles = []
player2_turtles = []
turtle_shapes = ["arrow", "turtle", "circle", "square", "triangle"]

# This for loop prints out player 1's turtles, each with different shapes, in their starting postions 
ystart1 = 100
for s in turtle_shapes:
    p1 = trtl.Turtle(shape = s)
    player1_turtles.append(p1)
    p1.penup()
    p1.speed(0)
    p1.fillcolor("red")
    p1.goto(187.5, ystart1)
    p1.setheading(180)
    ystart1 -= 50

# Mimics the above for loop, but for player 2 and the turtles face the opposite direction so that they are
# facing the opponent's lineup of turtles.
ystart2 = 100
for s in turtle_shapes:
    p2 = trtl.Turtle(shape = s)
    player2_turtles.append(p2)
    p2.penup()
    p2.speed(0)
    p2.fillcolor('blue')
    p2.goto(-187.5, ystart2)
    ystart2 -= 50

# This method will generate a random number between 1 and 5 to simulate a die roll and assign the 
# rolled value to a global variable, roll
def dieroll():

  min_value = 1
  max_value = 5
  global roll
  roll = random.randint(min_value, max_value)
  print ("You rolled a", roll)

# This method allows the player to choose which turtle they want to move and moves it as well using a
# long if statement and each turtle's x coordinate to determine which one was selected.  
def player1_move():
  turtle = int(input("Player 1, Choose a turtle to move"))
  distance = (37.5*roll)
  if (turtle == 1):
    turtle_y = 100
  elif (turtle == 2):
     turtle_y = 50
  elif (turtle == 3):
     turtle_y = 0
  elif (turtle == 4):
     turtle_y = -50
  elif (turtle == 5):
     turtle_y = -100
  for mt in player1_turtles:
    if (mt.ycor() == turtle_y):
      tempy = mt.ycor()
      mt.forward(distance)
      tempx = mt.xcor()
      mt.goto(tempx, tempy)

# The same as the above, but player 2 needs a different method to avoid confusion of 2 turtles
# on the same horizontal line moving. 
def player2_move():
  turtle = int(input("Player 2, Choose a turtle to move"))
  distance = (37.5*roll)
  if (turtle == 1):
    turtle_y = 100
  elif (turtle == 2):
     turtle_y = 50
  elif (turtle == 3):
     turtle_y = 0
  elif (turtle == 4):
     turtle_y = -50
  elif (turtle == 5):
     turtle_y = -100
  for mt in player2_turtles:
    if (mt.ycor() == turtle_y):
      tempy = mt.ycor()
      mt.forward(distance)
      tempx = mt.xcor()
      mt.goto(tempx, tempy)

# Update score will be checked frequently to see whether any turtles are on the finish line based on the
# x-coordinate of the turtles. It needs to iterate through both player lists and keep track in 2 different
# global variables for each player's score.
def update_score():
  for t in player1_turtles:
    if (t.xcor() == 0.0):
      global p1score
      p1score += 1

  for t in player2_turtles:
    if (t.xcor() == 0.0):
      global p2score
      p2score += 1

# The actual game. The loop seems really small because of the methods but is responsible for putting them
# together in a way that creates a real game. It also prints out the scores after every turn and resets them
# so that a scored turtle isn't counted as an extra point every turn.
# loop always runs since we have included break statements within it
while (1 == 1):
  dieroll()
  player1_move()
  dieroll()
  player2_move()
  update_score()
  print("Player 1 score: ", p1score)
  print("Player 2 score: ", p2score)
  # always checks for a win and breaks the loop in all combinations of a player reaching 3 points 
  if (p1score == 3):
    if (p2score != 3):
      print("Player 1 wins!")
      break
    elif (p2score == 3):
      print("Player 1 and 2 tied!")
      break
  elif (p2score == 3):
    print("Player 2 wins!")
    break
  # resets score so that a winning turtle will not count as a point every turn 
  p1score = 0
  p2score = 0

trtl.done()
