Нужно чтобы ИИ в морском бое старался потопить корабли, а подтопленные корабли обводил точками - C#
Формулировка задачи:
Нужно написать морской бой, но застрял на написании ИИ противника. Нужно чтобы ИИ старался потопить корабли, а подтопленные корабли обводил точками. Над ИИ долго мучился, но в итоге все равно корабли топит через раз. Просьба к знающим людям - посмотрите, в чем проблема? Может подскажете решение?
static Random _rnd = new Random(); public static Point ShotP = new Point(); public static Point Shot = new Point(); public static Point PrevShot = new Point(); public static bool LastFlag; public static int[,] Field = BattleMatrix.AIShotMatrix; // пустая матрица, "закрытое поле" ИИ public static bool ShootFlag = false; public static bool DrowFlag = false; public static int br = 0; public static List<Point> ShotList = new List<Point>(); public static Point NextShot = new Point(); public static void NewShot() { if (DrowFlag == false) { ShotP = new Point(_rnd.Next(0, 10), _rnd.Next(0, 10)); if (HumanBoard.Matrix[ShotP.X, ShotP.Y] == 0) { ShotFlagHuman.Missing = false; ShotFlagAI.Missing = true; HumanBoard.Matrix[ShotP.X, ShotP.Y] = 3; Field[ShotP.X, ShotP.Y] = 3; DrowFlag = false; } else if (HumanBoard.Matrix[ShotP.X, ShotP.Y] == 1) { ShotFlagHuman.Missing = true; ShotFlagAI.Missing = false; ShotFlagAI.AIFrag++; HumanBoard.Matrix[ShotP.X, ShotP.Y] = 4; Field[ShotP.X, ShotP.Y] = 4; Shot = ShotP; DrowFlag = true; } else if (HumanBoard.Matrix[ShotP.X, ShotP.Y] == 3) { ShotFlagHuman.Missing = true; ShotFlagAI.Missing = false; DrowFlag = false; NewShot(); } else if (HumanBoard.Matrix[ShotP.X, ShotP.Y] == 4) { ShotFlagHuman.Missing = true; ShotFlagAI.Missing = false; DrowFlag = false; NewShot(); } } if (DrowFlag == true) { if (Shot.X - 1 >= 0) { if (Field[Shot.X - 1, Shot.Y] == 0) { ShotList.Add(new Point(Shot.X - 1, Shot.Y)); } } if (Shot.X + 1 < 10) { if (Field[Shot.X + 1, Shot.Y] == 0) { ShotList.Add(new Point(Shot.X + 1, Shot.Y)); } } if (Shot.Y - 1 >= 0) { if (Field[Shot.X, Shot.Y - 1] == 0) { ShotList.Add(new Point(Shot.X, Shot.Y - 1)); } } if (Shot.Y + 1 < 10) { if (Field[Shot.X, Shot.Y + 1] == 0) { ShotList.Add(new Point(Shot.X, Shot.Y + 1)); } } if (ShotList.Count > 0) { NextShot = ShotList[_rnd.Next(0, ShotList.Count)]; } else { LastFlag = true; if (PrevShot.X > Shot.X) { if ((NextShot.X + (br + 1)) < 10) { NextShot.X += (br + 1); NextShot.Y = Shot.Y; } } if (PrevShot.X < Shot.X) { if ((NextShot.X - (br + 1)) >= 0) { NextShot.X -= (br + 1); NextShot.Y = Shot.Y; } } if (PrevShot.Y > Shot.Y) { if ((NextShot.Y + (br + 1)) < 10) { NextShot.X = Shot.X; NextShot.Y += (br + 1); } } if (PrevShot.Y < Shot.Y) { if ((NextShot.Y - (br + 1)) >= 0) { NextShot.X = Shot.X; NextShot.Y -= (br + 1); } } } if (LastFlag == true) { if (HumanBoard.Matrix[NextShot.X, NextShot.Y] == 0) { ShotFlagHuman.Missing = false; ShotFlagAI.Missing = true; HumanBoard.Matrix[NextShot.X, NextShot.Y] = 3; Field[NextShot.X, NextShot.Y] = 3; ShotList.Clear(); LastFlag = false; br = 0; DrowFlag = false; return; } else if (HumanBoard.Matrix[NextShot.X, NextShot.Y] == 1) { ShotFlagHuman.Missing = true; ShotFlagAI.Missing = false; ShotFlagAI.AIFrag++; HumanBoard.Matrix[NextShot.X, NextShot.Y] = 4; Field[NextShot.X, NextShot.Y] = 4; ShotList.Clear(); LastFlag = false; Shot = NextShot; NewShot(); } else if (HumanBoard.Matrix[NextShot.X, NextShot.Y] == 4) { ShotFlagHuman.Missing = true; ShotFlagAI.Missing = false; ShotList.Clear(); LastFlag = false; br = 0; DrowFlag = false; //Shot = NextShot; NewShot(); } else if (HumanBoard.Matrix[NextShot.X, NextShot.Y] == 3) { ShotFlagHuman.Missing = true; ShotFlagAI.Missing = false; ShotList.Clear(); LastFlag = false; br = 0; DrowFlag = false; //Shot = NextShot; NewShot(); } } if (HumanBoard.Matrix[NextShot.X, NextShot.Y] == 0) { ShotFlagHuman.Missing = false; ShotFlagAI.Missing = true; HumanBoard.Matrix[NextShot.X, NextShot.Y] = 3; Field[NextShot.X, NextShot.Y] = 3; ShotList.Clear(); return; } else if (HumanBoard.Matrix[NextShot.X, NextShot.Y] == 1) { ShotFlagHuman.Missing = true; ShotFlagAI.Missing = false; ShotFlagAI.AIFrag++; HumanBoard.Matrix[NextShot.X, NextShot.Y] = 4; Field[NextShot.X, NextShot.Y] = 4; ShotList.Clear(); PrevShot = Shot; Shot = NextShot; br++; NewShot(); } } }
Решение задачи: «Нужно чтобы ИИ в морском бое старался потопить корабли, а подтопленные корабли обводил точками»
textual
Листинг программы
public static void NewShot() { if (DrowFlag == false) { ShotP = new Point(_rnd.Next(0, 10), _rnd.Next(0, 10)); if (HumanBoard.Matrix[ShotP.X, ShotP.Y] == 0) { ShotFlagHuman.Missing = false; ShotFlagAI.Missing = true; HumanBoard.Matrix[ShotP.X, ShotP.Y] = 3; Field[ShotP.X, ShotP.Y] = 3; DrowFlag = false; } else if (HumanBoard.Matrix[ShotP.X, ShotP.Y] == 1) { ShotFlagHuman.Missing = true; ShotFlagAI.Missing = false; ShotFlagAI.AIFrag++; HumanBoard.Matrix[ShotP.X, ShotP.Y] = 4; Field[ShotP.X, ShotP.Y] = 4; Shot = ShotP; DrowFlag = true; } else if (HumanBoard.Matrix[ShotP.X, ShotP.Y] == 3) { ShotFlagHuman.Missing = true; ShotFlagAI.Missing = false; DrowFlag = false; NewShot(); } else if (HumanBoard.Matrix[ShotP.X, ShotP.Y] == 4) { ShotFlagHuman.Missing = true; ShotFlagAI.Missing = false; DrowFlag = false; NewShot(); } } if (DrowFlag == true) { if (WayChosen == false) { if (Shot.X - 1 >= 0) { if (Field[Shot.X - 1, Shot.Y] == 0) { ShotList.Add(new Point(Shot.X - 1, Shot.Y)); } } if (Shot.X + 1 < 10) { if (Field[Shot.X + 1, Shot.Y] == 0) { ShotList.Add(new Point(Shot.X + 1, Shot.Y)); } } if (Shot.Y - 1 >= 0) { if (Field[Shot.X, Shot.Y - 1] == 0) { ShotList.Add(new Point(Shot.X, Shot.Y - 1)); } } if (Shot.Y + 1 < 10) { if (Field[Shot.X, Shot.Y + 1] == 0) { ShotList.Add(new Point(Shot.X, Shot.Y + 1)); } } if (ShotList.Count > 0) { NextShot = ShotList[_rnd.Next(0, ShotList.Count)]; } else { DrowFlag = false; return; } } /*if (WayChosen == true) { NextShot; }*/ if (HumanBoard.Matrix[NextShot.X, NextShot.Y] == 0) { ShotFlagHuman.Missing = false; ShotFlagAI.Missing = true; HumanBoard.Matrix[NextShot.X, NextShot.Y] = 3; Field[NextShot.X, NextShot.Y] = 3; ShotList.Clear(); if (WayChosen == true) { if (NextShot.X > ShotP.X) { NextShot.X = ShotP.X - 1; WayChosen = true; } if (NextShot.X < ShotP.X) { NextShot.X = ShotP.X + 1; WayChosen = true; } if (NextShot.Y > ShotP.Y) { NextShot.Y = ShotP.Y - 1; WayChosen = true; } if (NextShot.Y < ShotP.Y) { NextShot.Y = ShotP.Y + 1; WayChosen = true; } } //WayChosen = false; return; } if (HumanBoard.Matrix[NextShot.X, NextShot.Y] == 1) { ShotFlagHuman.Missing = true; ShotFlagAI.Missing = false; ShotFlagAI.AIFrag++; HumanBoard.Matrix[NextShot.X, NextShot.Y] = 4; Field[NextShot.X, NextShot.Y] = 4; ShotList.Clear(); if (NextShot.X > Shot.X) { NextShot.X++; WayChosen = true; } if (NextShot.X < Shot.X) { NextShot.X--; WayChosen = true; } if (NextShot.Y > Shot.Y) { NextShot.Y++; WayChosen = true; } if (NextShot.Y < Shot.Y) { NextShot.Y--; WayChosen = true; } //Shot = NextShot; //br++; NewShot(); } if (HumanBoard.Matrix[NextShot.X, NextShot.Y] == 3) { ShotFlagHuman.Missing = false; ShotFlagAI.Missing = true; ShotList.Clear(); WayChosen = false; DrowFlag = false; NewShot(); } if (HumanBoard.Matrix[NextShot.X, NextShot.Y] == 4) { ShotFlagHuman.Missing = false; ShotFlagAI.Missing = true; ShotList.Clear(); DrowFlag = false; WayChosen = false; NewShot(); } } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д