過河問題:農夫、羊、狼、青菜
一名農夫帶著一隻狼、一隻羊和一顆高麗菜渡河,河上只有一艘船,一次只能載農夫和一樣東西到對岸,只有農夫會/能划船,如果農夫沒看著,狼會吃羊,羊會吃菜,農夫要如何成功的將狼、羊、高麗菜帶到另一岸,而不會有損失?
程式畫面
【程式列表】
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { string farmerLocation = "L"; //農夫的位置,一開始在左邊 public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { lstBoxL.Enabled = true; L.Text = "西岸(可選取)"; lstBoxR.Enabled = false; R.Text = "東岸(不可選取)"; } /* * 當按了往右邊移動,有三個狀況: * 1.若農夫已在右邊,不需要移動,離開 * 2.只有農夫自己一人過河 * 3.農夫帶著一物過河,選取的物品從左邊移入右邊(Add),並從左邊移除(Remove) * * 2與3共同步驟: * 設定農夫位值在右邊 * 對調左右2個清單方塊的可選取狀態 */ private void btn2Right_Click(object sender, EventArgs e) //按下往右移動 { if (farmerLocation == "R") return; //如果農夫已在右邊的話就離開 if (lstBoxL.SelectedItem != null) //若左邊方塊有選取 { lstBoxR.Items.Add(lstBoxL.SelectedItem); //將左邊方塊的選取項目加到右邊方塊 lstBoxL.Items.Remove(lstBoxL.SelectedItem); //將左邊方塊選取項目移除掉(移到另一邊的意思) } lstBoxL.Enabled = false; L.Text = "西岸(不可選取)"; lstBoxR.Enabled = true; R.Text = "東岸(可選取)"; btnFarmer.Location = new Point(312, 120); //移動農夫 farmerLocation = "R"; //標記農夫的位置在右邊 } private void btn2Left_Click(object sender, EventArgs e) { if (farmerLocation == "L") return; //如果農夫已在右邊的話就離開 if (lstBoxR.SelectedItem != null) { lstBoxL.Items.Add(lstBoxR.SelectedItem); lstBoxR.Items.Remove(lstBoxR.SelectedItem); } lstBoxR.Enabled = false; R.Text = "東岸(不可選取)"; lstBoxL.Enabled = true; L.Text = "西岸(可選取)"; btnFarmer.Location = new Point(192, 120); farmerLocation = "L"; } private void button1_Click(object sender, EventArgs e) { //取消二邊清單方塊的選取 lstBoxL.SelectedItem = null; lstBoxR.SelectedItem = null; } } }
【程式英文】
1.ListBox 清單方塊 2.Item 項目 (清單方塊裏的) 3.Items 項目集合 4.Enabled 啟用的 5.SelectedItem 選取的項目 6.null 未指定/空 7.Location 位置 8.point 點【遊戲判斷】
1.測試清單方塊裏的項目是否同時出現? 當農夫在另一邊時,此時,遊戲失敗!a.羊和高麗菜,羊會吃了高麗菜
b.狼和羊,狼會吃了羊
2.若羊、狼、高麗菜同時出現在右邊清單方塊時,此時,遊戲過關!【程式判斷是否失敗】
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication2 { public partial class Form1 : Form { string farmerLocation = "L"; //農夫的位置,一開始在左邊 public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { lstBoxL.Enabled = true; L.Text = "西岸(可選取)"; lstBoxR.Enabled = false; R.Text = "東岸(不可選取)"; } private void lstBoxL_SelectedIndexChanged(object sender, EventArgs e) { } private bool isGameFailed() { bool hasSheep = false, hasWolf = false, hasCab = false; if (farmerLocation == "L") { foreach (object o in lstBoxR.Items) { string s = o.ToString(); if (s == "羊") hasSheep = true; if (s == "狼") hasWolf = true; if (s == "高麗菜") hasCab = true; } } else { foreach (object o in lstBoxL.Items) { string s = o.ToString(); if (s == "羊") hasSheep = true; if (s == "狼") hasWolf = true; if (s == "高麗菜") hasCab = true; } } bool gameFailed = false; if (hasSheep && hasCab) gameFailed = true; if (hasSheep && hasWolf) gameFailed = true; return gameFailed; } private void btn2Left_Click(object sender, EventArgs e) { if (farmerLocation == "L") return; //如果農夫已在右邊的話就離開 if (lstBoxR.SelectedItem != null) { lstBoxL.Items.Add(lstBoxR.SelectedItem); lstBoxR.Items.Remove(lstBoxR.SelectedItem); } lstBoxR.Enabled = false; R.Text = "東岸(不可選取)"; lstBoxL.Enabled = true; L.Text = "西岸(可選取)"; btnFarmer.Location = new Point(168, 101); farmerLocation = "L"; if (isGameFailed()) MessageBox.Show("You losed!"); } private void btn2Right_Click(object sender, EventArgs e) { if (farmerLocation == "R") return; //如果農夫已在右邊的話就離開 if (lstBoxL.SelectedItem != null) //若左邊方塊有選取 { lstBoxR.Items.Add(lstBoxL.SelectedItem); //將左邊方塊的選取項目加到右邊方塊 lstBoxL.Items.Remove(lstBoxL.SelectedItem); //將左邊方塊選取項目移除掉(移到另一邊的意思) } lstBoxL.Enabled = false; L.Text = "西岸(不可選取)"; lstBoxR.Enabled = true; R.Text = "東岸(可選取)"; btnFarmer.Location = new Point(312, 101); //移動農夫 farmerLocation = "R"; //標記農夫的位置在右邊 if (isGameFailed()) MessageBox.Show("You losed!"); } private void button1_Click(object sender, EventArgs e) { //取消二邊清單方塊的選取 lstBoxL.SelectedItem = null; lstBoxR.SelectedItem = null; } } }