private bool DoIt(string command, int materialInt, ref bool complette, ref bool clickStop) { if (string.Compare(command, "Go();", StringComparison.CurrentCultureIgnoreCase) == 0) { // param = 1; // один шаг // contr.Move(ref Cat1, ref Cat2, this.Size.Width, ref complette, ref CatStay, ref param, ref clickStop, ref materialInt); contr.MoveOneStep(ref Cat1, ref Cat2, this.Size.Width, ref complette, ref CatStay, ref clickStop, ref materialInt, ref labelINFO); } else if (string.Compare(command, "Jump();", StringComparison.CurrentCultureIgnoreCase) == 0) { int mat = level.GetMaterial(); contr.Jump(ref Cat1, ref Cat2, this.Size.Width, ref complette, ref CatStay, ref clickStop, mat); } else if (string.Compare(command, "Attack();", StringComparison.CurrentCultureIgnoreCase) == 0) { Cat1.Visible = false; Cat2.Visible = false; CatStay.Location = Cat2.Location; CatStay.Visible = true; bool existHolzBlock; contr.Attack(ref CatStay, ref clickStop, out existHolzBlock, ref labelINFO); if (level.GetMaterial() == 2 && existHolzBlock) { holzBlock.Visible = false; if (level.levelLabel().Equals("Level IV")) { magicHolz.Visible = false; } } } else { ClassSound.playError(); labelError.Visible = true; labelError.Text += "\nНеизвестная команда " + command; labelError.Visible = true; timer1.Enabled = false; clickStop = true; return(false); } return(true); //выполнилась без ошибок }
private void buttonRun_Click(object sender, EventArgs e) { labelNextLevel.Visible = false; if (labelLevel.Text.Equals("Level III")) { holzBlock.Visible = true; } clickStop = false; labelLevelComplette.Visible = false; pictureBox1.Image = GameKitten_sAdventure.Properties.Resources.diamant2; contr.ReturnBeginPosition(ref CatStay, this.Width); contr.ReturnBeginPosition(ref Cat1, this.Width); contr.ReturnBeginPosition(ref Cat2, this.Width); int materialIntIf = 0; string command; String[] s = textBox1.Text.Split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); labelINFO.Visible = true; labelINFO.Text = ""; bool complette = false; for (int i = 0; i < s.Length; i++) { command = ""; materialIntIf = 0;//неизвестно, какой материал if (!complette || !clickStop) { //if (...) then Regex regexIf = new Regex(@"if\s*\(\s*(wooden block|stone block)\s*\)\sthen\s*[A-Z|a-z|;|(|)]*$", RegexOptions.IgnoreCase); //if (...) then ... else ... Regex regexIfElse = new Regex(@"if\s*\(\s*(wooden block|stone block)\s*\)\s*then\s*[A-Z|a-z|;|(|)]*\s*else\s*[A-Z|a-z|;|(|)]*", RegexOptions.IgnoreCase); //while (...) do { ... } Regex regexWhile = new Regex(@"while\s*\(\s*(free)\s*\)\s*do\s*{", RegexOptions.IgnoreCase); s[i] = s[i].ToLower(); //s[i] = s[i].Replace(" ", ""); if (regexIf.IsMatch(s[i])) { int begPos = s[i].IndexOf("then"); int endPos = s[i].IndexOf(';'); int length = endPos - begPos - 4; int temp = 0; command = ""; //Команда, выполняемая, если условие выполнено for (int j = begPos + 5; j < endPos; j++) { if (char.IsLetter(s[i][j])) { command = s[i].Substring(j, length - temp); break; } temp++; } Match match = regexIf.Match(s[i]); if (match.Groups[1].Value == "wooden block") { materialIntIf = 2; } if (match.Groups[1].Value == "stone block") { materialIntIf = 1; } if (level.GetMaterial() == materialIntIf) { DoIt(command, materialIntIf, ref complette, ref clickStop); } } else if (regexIfElse.IsMatch(s[i])) { int posThen = s[i].IndexOf("then"); int posElse = s[i].IndexOf("else"); string commandThen = s[i].Substring(posThen + 5, posElse - posThen - 6); string commandElse = s[i].Substring(posElse + 5, s[i].Length - posElse - 5); int begPosCond = s[i].IndexOf("("); int endPosCond = s[i].IndexOf(")"); string condition = s[i].Substring(begPosCond + 1, endPosCond - begPosCond - 1); if (condition == "wooden block") { materialIntIf = 2; } if (condition == "stone block") { materialIntIf = 1; } //labelINFO.Text = commandThen +" " + commandElse; if (level.GetMaterial() == materialIntIf) //Если условие выполняется, то выполняем команду после then { DoIt(commandThen, materialIntIf, ref complette, ref clickStop); } else //иначе после else { DoIt(commandElse, materialIntIf, ref complette, ref clickStop); } } else if (regexWhile.IsMatch(s[i])) { int begPos = s[i].IndexOf("{"); int endPos = s[i].IndexOf("}"); string allComand = s[i].Substring(begPos + 1, endPos - begPos - 1); char[] delimiter = { ' ' }; string[] commands = allComand.Split(delimiter, StringSplitOptions.RemoveEmptyEntries); int begPosCond = s[i].IndexOf("("); int endPosCond = s[i].IndexOf(")"); string condition = s[i].Substring(begPosCond + 1, endPosCond - begPosCond - 1); condition = condition.Replace(" ", ""); materialIntIf = 0; if (condition == "free") { while (!clickStop && !contr.IsExistLet(level, ref labelLevel, ref Cat1, ref Cat2) && !contr.IsExistDiamond(level, ref labelLevel, ref Cat1, ref Cat2)) // while (!clickStop && !contr.IsExistDiamond(level, ref labelLevel, ref Cat1, ref Cat2)) { for (int j = 0; j < commands.Length; j++) { // if (!clickStop && !contr.IsExistDiamond(level, ref labelLevel, ref Cat1, ref Cat2)) if (!clickStop && !contr.IsExistLet(level, ref labelLevel, ref Cat1, ref Cat2) && !contr.IsExistDiamond(level, ref labelLevel, ref Cat1, ref Cat2)) { commands[j] = commands[j].Replace(" ", ""); DoIt(commands[j], materialIntIf, ref complette, ref clickStop); } } } CatStay.Location = Cat1.Location; CatStay.Location = Cat2.Location; Cat1.Visible = false; Cat2.Visible = false; CatStay.Visible = true; if (labelLevel.Text.Equals("Level IV")) { if (level.ExistLet(Cat1, Cat2) && !contr.GetRuinBlock4()) { Random rnd = new Random(); int materialInt = rnd.Next(1, 3); ChangeMagicBlock(materialInt); level.SetMaterial(materialInt); } } } } else { DoIt(s[i], materialIntIf, ref complette, ref clickStop); } } } if (complette) { ClassSound.playWin(); pictureBox1.Image = GameKitten_sAdventure.Properties.Resources.diamantComplette; labelLevelComplette.Visible = true; labelNextLevel.Visible = true; if (labelLevel.Text.Equals("Level IV")) { labelNextLevel.Text = "Следующие уровни - скоро!"; } } CatStay.Location = Cat1.Location; CatStay.Location = Cat2.Location; Cat1.Visible = false; Cat2.Visible = false; CatStay.Visible = true; }