Created
January 17, 2017 21:01
-
-
Save lemusthelroy/52cf53604ef08d62c7f2a0382d22da7a to your computer and use it in GitHub Desktop.
Chicken, Fox, Feed Riddle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.UI; | |
using System.Web.UI.WebControls; | |
public partial class webpages_game : System.Web.UI.Page | |
{ | |
private string chickenPosition; | |
private string foxPosition; | |
private string feedPosition; | |
public List<Cargo> cargo = new List<Cargo>(); | |
public bool gameOver = false; | |
public Game CurrentGame = new Game(); | |
protected void Page_Load(object sender, EventArgs e) | |
{ | |
cargo.Add(new Cargo(name: "Chicken", preyTo: "Fox", message: "The Chicken has been chomped to death by the fox.", | |
ischecked: (chicken_cb.Checked) | |
)); | |
cargo.Add(new Cargo(name: "Fox", preyTo: null, ischecked: (fox_cb.Checked))); | |
cargo.Add(new Cargo(name: "Feed", preyTo: "Chicken", message: "The feed has been ravaged by the chicken.", | |
ischecked: (feed_cb.Checked))); | |
if (!IsPostBack) | |
{ | |
SetValues(); | |
} | |
else | |
{ | |
if (!BoatScales()) | |
{ | |
GameMessage.Text = ""; | |
FormFunction(); | |
} | |
if (!OnTheBank()) | |
{ | |
RiverBank(); | |
SetValues(); | |
} | |
} | |
} | |
private void SetValues() | |
{ | |
// If items are on that side of the river, display them there // | |
foreach (var crg in cargo) | |
{ | |
if (crg.name == "Chicken") | |
{ | |
LeftChicken.Visible = (crg.position == "1"); | |
RightChicken.Visible = (crg.position == "2"); | |
} | |
if (crg.name == "Fox") | |
{ | |
LeftFox.Visible = (crg.position == "1"); | |
RightFox.Visible = (crg.position == "2"); | |
} | |
if (crg.name == "Feed") | |
{ | |
LeftFeed.Visible = (crg.position == "1"); | |
RightFeed.Visible = (crg.position == "2"); | |
} | |
} | |
} | |
private void FormFunction() | |
{ | |
foreach (var crg in cargo) | |
{ | |
if (crg.position == BoatSide.Value && crg.ischecked) | |
{ | |
if (crg.position == "1") | |
{ | |
crg.position = "2"; | |
} | |
else | |
{ | |
crg.position = "1"; | |
} | |
} | |
else if (crg.position != BoatSide.Value && crg.ischecked) | |
{ | |
GameMessage.Text = "This item is not on your side of the river"; | |
} | |
else | |
{ | |
} | |
} | |
} | |
private bool BoatScales() | |
{ | |
bool overweight = false; | |
if (Truth(feed_cb.Checked, fox_cb.Checked, chicken_cb.Checked) > 1) | |
{ | |
overweight = true; | |
CurrentGame.ValidMove = false; | |
GameMessage.Text = "You may only take one item on the boat!"; | |
} | |
else | |
{ | |
overweight = false; | |
} | |
return overweight; | |
} | |
private bool OnTheBank() | |
{ | |
bool LethalCombo = false; | |
foreach (var item in cargo) | |
{ | |
if (item.position != BoatSide.Value) | |
{ | |
foreach (var item2 in cargo) | |
{ | |
if (item2.preyTo == item.name && item.position == item2.position) | |
{ | |
LethalCombo = true; | |
gameOver = true; | |
GameMessage.Text = item2.message; | |
} | |
} | |
} | |
} | |
return LethalCombo; | |
} | |
private void RiverBank() | |
{ | |
if (BoatSide.Value == "1") | |
{ | |
BoatSide.Value = "2"; | |
} | |
else if (BoatSide.Value == "2") | |
{ | |
BoatSide.Value = "1"; | |
} | |
} | |
public static int Truth(params bool[] booleans) | |
{ | |
return booleans.Count(b => b); | |
} | |
} | |
public class Game | |
{ | |
bool validMove = true; | |
string gameName; | |
string player; | |
public bool ValidMove | |
{ | |
get | |
{ | |
return validMove; | |
} | |
set | |
{ | |
validMove = value; | |
} | |
} | |
public string GameName { get; set; } | |
public string Player { get; set; } | |
} | |
public class Cargo | |
{ | |
public Cargo() { } | |
public Cargo(string name, string preyTo, bool ischecked) | |
{ | |
this.name = name; | |
this.preyTo = preyTo; | |
this.position = "1"; | |
} | |
public Cargo(string name, string preyTo, bool ischecked, string message) | |
{ | |
this.name = name; | |
this.preyTo = preyTo; | |
this.message = message; | |
this.position = "1"; | |
} | |
public Cargo(string name, string preyTo, bool ischecked, string message, string position) | |
{ | |
this.name = name; | |
this.preyTo = preyTo; | |
this.message = message; | |
this.position = position; | |
} | |
public string name { get; set; } | |
public string preyTo { get; set; } | |
public bool ischecked { get; set; } | |
public string position; | |
public string message { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment