Added a merge sort

This commit is contained in:
Anthony Berg 2020-08-24 00:58:19 +01:00
parent bad24381f2
commit 6775b97073
3 changed files with 181 additions and 59 deletions

View File

@ -2,10 +2,12 @@
using JetBrains.Annotations;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Newtonsoft.Json;
using UnityEditor;
using UnityEngine.UI;
using UnityEngine.XR;
public class Property
@ -98,14 +100,16 @@ public class Board //Creating the class for the board mechanics.
public Board(List<Player> players, List<Property> properties)
{
this.players = players;
Debug.Log(this.players.Count);
textHandler = GameObject.FindObjectOfType<TextHandler>();
buttonHandler = GameObject.FindObjectOfType<ButtonHandler>();
this.players = players; //Imports all of the players playing
Debug.Log(this.players.Count); //Prints how many players are playing
textHandler = GameObject.FindObjectOfType<TextHandler>(); //Finds the text handler script
buttonHandler = GameObject.FindObjectOfType<ButtonHandler>(); //Finds the button handler script
existingProperties = properties; //This sets all of the properties that exist
houses = 32; //Defining the amount of houses - They have a finite amount
hotels = 12; //Defining the amount of hotels - They have a finite amount
totalProperties = 28;
totalProperties = 28; //Sets the total properties in the game
//Creates a list for all the buyable properties
for (int i=0; i < properties.Count; i++)
{
if (properties[i].isBuyable())
@ -115,52 +119,55 @@ public class Board //Creating the class for the board mechanics.
}
}
public void MovePlayer(int roll)
public void MovePlayer(int roll) //This moves the player
{
players[currentPlayer].Move(roll);
textHandler.updateRoll(roll);
players[currentPlayer].Move(roll); //This is telling the player to move in the local player class.
textHandler.updateRoll(roll); //This is updating the text on the screen for the roll
int money = players[currentPlayer].money;
textHandler.updateMoney(money);
//Money UI
int money = players[currentPlayer].money; //Gets the money the player has
textHandler.updateMoney(money); //Updates the money on the UI
string propertyName = existingProperties[players[currentPlayer].position].property_name;
int propertyValue = existingProperties[players[currentPlayer].position].property_value;
//Property UI
string propertyName = existingProperties[players[currentPlayer].position].property_name; //Gets the property name from where the player is at
int propertyValue = existingProperties[players[currentPlayer].position].property_value; //Gets the value of the property of where the player is at
if (propertyValue != 0)
if (propertyValue != 0) //This is for if the property can be bought
{
textHandler.updateProperty(propertyName, propertyValue);
textHandler.updateProperty(propertyName, propertyValue); //Updates the UI text for the property info
}
else
{
textHandler.updateTile(propertyName); //property_value
textHandler.updateTile(propertyName); //Updates the UI text for the property info without the value of the property
}
buttonHandler.disableRollDice();
buttonHandler.disableRollDice(); //Disables the user from being able to roll the dice whilst the player is moving
}
public void CheckFees()
public void CheckFees() //This checks if the player has to pay for landing on a property
{
int playerPosition = players[currentPlayer].position;
Property currentProperty = existingProperties[playerPosition];
int playerPosition = players[currentPlayer].position; //It gets the current position of the player on the board
Property currentProperty = existingProperties[playerPosition]; //It gets the property that it is currently on
if (currentProperty.property_rent >= 100)
//This checks if the player landed on a tax tile
if (currentProperty.property_rent >= 100) //Only tax tiles have a rent value of 100 or 200
{
players[currentPlayer].Pay(Convert.ToInt32(currentProperty.property_rent));
players[currentPlayer].Pay(Convert.ToInt32(currentProperty.property_rent)); //Makes the player pay the tax and converts int? to int
}
textHandler.updateMoney(players[currentPlayer].money);
textHandler.updateMoney(players[currentPlayer].money); //Updates the UI for the current amount of money the player has.
}
public void NextPlayer()
public void NextPlayer() //This moves the queue to the next player.
{
if (currentPlayer + 1 >= players.Count)
if (currentPlayer + 1 >= players.Count) //If the counter is about to overflow in the queue, then...
{
Debug.Log("Restarted");
currentPlayer = 0;
Debug.Log("Restarted"); //Prints that the queue has started from the start
currentPlayer = 0; //Starts the queue at 0 again.
}
else
{
currentPlayer++;
currentPlayer++; //Increments the queue to the next player
}
}
@ -186,14 +193,14 @@ public class Board //Creating the class for the board mechanics.
{
int position = players[currentPlayer].position; //This is the current position of the player for the property.
Property property = existingProperties[position]; //This gets the property that the player is buying
int money = players[currentPlayer].money;
int money = players[currentPlayer].money; //Gets the current amount of money the player has
if (money - property.property_value < 0)
if (money - property.property_value < 0) //Checks if the player doesn't have enough money to pay for it
{
Debug.Log("The player doesn't have enough money!");
buttonHandler.disableBuying(); //Removes the buy button.
buttonHandler.enableRollDice(); //Re-enables the user to roll the dice.
return;
return; //Stops the function
}
for (int i = 0; i < avaliableProperties.Count; i++) //Checks through all of the properties that are buyable using a linear search
@ -205,41 +212,42 @@ public class Board //Creating the class for the board mechanics.
textHandler.updateMoney(players[currentPlayer].money); //This updates the amount of money the player has.
buttonHandler.disableBuying(); //Removes the buy button.
buttonHandler.enableRollDice(); //Re-enables the user to roll the dice.
return;
return; //Stops the function
}
}
Debug.Log("The property cannot be bought!");
Debug.Log("The property cannot be bought!"); //Prints that theres an error
}
}
public class Player
{
private string name;
private int playerNumber;
private string name; //This is the username of the player
private int playerNumber; //This is the player number in the queue
public int money; //Initializes the variable for money.
public int position; //Positions vary from 0-39 (40 squares on the board) (Go is 0)
public bool inJail;
public List<Property> ownedProperties = new List<Property>();
public bool inJail; //This enables specific in jail functions
public List<Property> ownedProperties; //This is the list of properties that the player owns.
public GameObject player;
private Movement movement;
public Player(string playerName, int playerNumber, GameObject player)
{
name = playerName;
position = 0;
inJail = false;
this.playerNumber = playerNumber;
name = playerName; //This initialises the username of the player
position = 0; //This sets to the default position - GO
inJail = false; //This initialises that the player isn't in jail
this.playerNumber = playerNumber; //This is the position in the queue that the player is in
money = 1500; //Set the default starting money.
this.player = player;
movement = GameObject.FindObjectOfType<Movement>();
this.player = player; //This links the object that the player is linked to in the game
ownedProperties = new List<Property>();
movement = GameObject.FindObjectOfType<Movement>(); //This finds the movement script in the game
}
public void Move(int roll) //This moves the player a certain length (what they got from rolling the dice).
{
int previousPosition = position;
int previousPosition = position; //This saves the previous position the player was at
position += roll; //Add the position with what was rolled.
@ -249,7 +257,7 @@ public class Player
money += 200; //Collect money as they pass go.
}
movement.Move(previousPosition, position, playerNumber);
movement.Move(previousPosition, position, playerNumber); //This moves the player.
//return position; //Returns where the player needs to move to on the board
}
@ -257,27 +265,27 @@ public class Player
public void GoToJail() //If the player needs to go to jail.
{
int previousPosition = position;
Debug.Log("Jailed!!!");
Debug.Log("Jailed!!!"); //Prints a message to say that the player is in jail.
position = 40; //Special position for jail.
inJail = true;
inJail = true; //Enables the in jail functions.
movement.Move(previousPosition,position, playerNumber); //Moves the player to jail.
}
public void GetOutOfJail(int roll) //If the player is going out of jail.
{
position = 10; //Moves the player out of jail.
inJail = false;
inJail = false; //Disables the inJail functions for the player.
Move(roll); //Then moves the player.
}
public void BuyProperty(Property property) //This function allows the player to own a property.
{
int price = property.property_value;
if (money - price >= 0)
{
ownedProperties.Add(property); //Adds the property to the list of the player owned properties.
ownedProperties = MergeMethod.MergeSort(ownedProperties);
money -= price;
}
else
@ -286,6 +294,26 @@ public class Player
}
}
public void CheckColourSet(string colour) //Checks if the player has a whole colour set.
{
int required = 3; //This is the number of properties needed to own to buy houses.
int counter = 0; //This checks how many times the property is found.
if (colour == "brown" || colour == "dark blue") //Only brown and dark blue has 2 properties.
{
required = 2;
}
for (int i = 0; i < ownedProperties.Count; i++)
{
//TODO Use the merge sort to make the for loop more efficient.
}
}
public void Pay(int fee)
{
money -= fee;
@ -293,6 +321,75 @@ public class Player
}
public static class MergeMethod
{
public static List<Property> MergeSort(List<Property> unsorted)
{
if (unsorted.Count <= 1) //Checks if the list is longer than 1 to do a merge sort
{
return unsorted; //Stops the function if the length is 1 or less
}
int middle = unsorted.Count / 2; //Does an integer division of 2
List<Property> left = new List<Property>(); //Creates a list for the left items in the list.
List<Property> right = new List<Property>(); //Creates a list for the right items in the list.
for (int i = 0; i < middle; i++) //Adds the left half of the unsorted list to the left list.
{
left.Add(unsorted[i]);
}
for (int i = middle; i < unsorted.Count; i++) //Adds the rest of the unsorted list to the right list.
{
right.Add(unsorted[i]);
}
//Uses recursion to get to return early.
left = MergeSort(left);
right = MergeSort(right);
//Merges the lists.
return Merge(left, right);
}
private static List<Property> Merge(List<Property> left, List<Property> right)
{
List<Property> sorted = new List<Property>(); //Creates the list with the sort.
while (left.Count > 0 || right.Count > 0) //While operates as the left and right lists aren't empty.
{
if (left.Count > 0 && right.Count > 0) //Checks if none of the lists are empty.
{
if (left.First().property_id <= right.First().property_id) //Checks if the left one is smaller than the right one.
{
sorted.Add(left.First());
left.Remove(left.First());
}
else //If the right one is smaller than the left one then...
{
sorted.Add(right.First());
right.Remove(right.First());
}
}
else if (left.Count > 0) //Runs if the only list left is the left one.
{
sorted.Add(left.First());
left.Remove(left.First());
}
else if (right.Count > 0) //Runs if the only list left is the right one.
{
sorted.Add(right.First());
right.Remove(right.First());
}
}
return sorted; //Returns the sorted list.
}
}
public class Main : MonoBehaviour
{
private List<Property> existingProperties;
@ -307,7 +404,6 @@ public class Main : MonoBehaviour
players.Add(new Player("smyalygames", 0, GameObject.Find("/Players/Player1")));
players.Add(new Player("coomer", 1, GameObject.Find("/Players/Player2")));
Debug.Log(players[0].player.name);
existingProperties = JsonConvert.DeserializeObject<List<Property>>(FileHandler.LoadProperties());
board = new Board(players, existingProperties);
}

View File

@ -52,6 +52,6 @@ public class RollDice : MonoBehaviour
}
main.board.MovePlayer(4);
main.board.MovePlayer(totalRoll);
}
}

File diff suppressed because one or more lines are too long