Класс FieldModel переработан под любой размер
This commit is contained in:
parent
96f58b3d85
commit
2d01e5b2b9
@ -1,15 +1,12 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
//using static UnityEngine.Rendering.DebugUI.Table;
|
||||
|
||||
//êåì çàíÿò/íå çàíÿò ñëîò
|
||||
public enum CellState
|
||||
{
|
||||
Empty, Player1, Player2
|
||||
}
|
||||
public class FieldModel
|
||||
{
|
||||
|
||||
//äâóìåðíûé ìàññèâ äëÿ ïîëÿ
|
||||
public CellState[,] fieldArr;
|
||||
public int height, width;
|
||||
@ -31,13 +28,6 @@ public class FieldModel
|
||||
{
|
||||
fieldArr = new CellState[height, width];
|
||||
Array.Clear(fieldArr, 0, fieldArr.Length);
|
||||
//for (int row = 0; row < height; row++)
|
||||
//{
|
||||
// for (int col = 0; col < width; col++)
|
||||
// {
|
||||
// fieldArr[row, col] = CellState.Empty;
|
||||
// }
|
||||
//}
|
||||
}
|
||||
|
||||
//Îñíîâíûå ïðîâåðêè ñ ðàçâåðíóòûì ðåçóëüòàòîì
|
||||
@ -70,7 +60,7 @@ public class FieldModel
|
||||
|
||||
|
||||
int lineLength = 0;
|
||||
for (int col = 0; col < 7; col++)
|
||||
for (int col = 0; col < width; col++)
|
||||
{
|
||||
CellState cell = fieldArr[row, col];
|
||||
if (cell != currentPlayer)
|
||||
@ -101,7 +91,7 @@ public class FieldModel
|
||||
{
|
||||
CellState currentPlayer = GameManager.instance.currentPlayer;
|
||||
int lineLength = 0;
|
||||
for (int row = 5; row >= 0; row--)
|
||||
for (int row = height - 1; row >= 0; row--)
|
||||
{
|
||||
CellState cell = fieldArr[row, column];
|
||||
if (cell != currentPlayer)
|
||||
@ -113,12 +103,8 @@ public class FieldModel
|
||||
|
||||
if (lineLength == 4)//íàøëè ðÿä èç 4 ôèøåê
|
||||
{
|
||||
//int[] indexesRow = new int[4];
|
||||
//int[] indexesColumn = new int[4];
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
//indexesColumn[i] = column;
|
||||
//indexesRow[i] = row + i;
|
||||
tempIndexesColumn[i] = column;
|
||||
tempIndexesRow[i] = row + i;
|
||||
}
|
||||
@ -132,12 +118,14 @@ public class FieldModel
|
||||
{
|
||||
CellState currentPlayer = GameManager.instance.currentPlayer;
|
||||
int sum = row + col;
|
||||
if (sum <= 2 || sum >=9) //ïðè òàêîé ñóììå èíäåêñîâ 4 ôèøêè â äèàãîíàëü íå ïîìåùàþòñÿ
|
||||
|
||||
// Äèàãîíàëü ñëèøêîì êîðîòêàÿ, åñëè ñóììà ìåíüøå ìèíèìàëüíîé (3) èëè áîëüøå ìàêñèìàëüíîé (height+width-4)
|
||||
if (sum < 3 || sum > height + width - 4)
|
||||
return new CheckResult(false, false);
|
||||
|
||||
//ñ÷èòàåì èíäåêñ íà÷àëà äèàãîíàëè (ëåâî íèç)
|
||||
int startRow = Mathf.Clamp(sum, 0, 5);
|
||||
int startCol = Mathf.Clamp(sum - 5, 0, 5);
|
||||
// Âû÷èñëÿåì íà÷àëüíóþ òî÷êó äèàãîíàëè
|
||||
int startRow = Mathf.Clamp(sum, 0, height - 1);
|
||||
int startCol = Mathf.Clamp(sum - (height - 1), 0, width - 1);
|
||||
|
||||
int lineLength = 0;
|
||||
|
||||
@ -150,35 +138,30 @@ public class FieldModel
|
||||
continue;
|
||||
}
|
||||
lineLength++;
|
||||
|
||||
if (lineLength == 4)//êîìáèíàöèÿ èç 4 íàøëàñü)
|
||||
if (lineLength == 4)
|
||||
{
|
||||
//int[] indexesRow = new int[4];
|
||||
//int[] indexesColumn = new int[4];
|
||||
for (int i = 0; i < 4; i++)
|
||||
{//èäåì íàçàä è çàïîëíÿåì èíäåêñû
|
||||
//indexesColumn[i] = col-i;
|
||||
//indexesRow[i] = row + i;
|
||||
{
|
||||
tempIndexesRow[i] = row + i;
|
||||
tempIndexesColumn[i] = col-i;
|
||||
tempIndexesColumn[i] = col - i;
|
||||
}
|
||||
return new CheckResult(true, false, tempIndexesRow, tempIndexesColumn);
|
||||
}
|
||||
}
|
||||
return new CheckResult(false, false);//íå íàøëè êîìáèíàöèþ
|
||||
return new CheckResult(false, false);
|
||||
}
|
||||
public CheckResult checkDiagonalUpDown(int row, int col)
|
||||
{
|
||||
CellState currentPlayer = GameManager.instance.currentPlayer;
|
||||
int sub = row - col;
|
||||
int startRow = Mathf.Clamp(sub, 0, 2);
|
||||
int startCol = Mathf.Abs(Math.Clamp(sub, -3, 0));
|
||||
|
||||
//CellState[] diagonalArray = getUpDownDiagonal(row, col);
|
||||
// Íà÷àëüíàÿ òî÷êà äèàãîíàëè, ïðîõîäÿùåé ÷åðåç (row, col)
|
||||
int startRow = sub >= 0 ? sub : 0;
|
||||
int startCol = sub >= 0 ? 0 : -sub;
|
||||
|
||||
int lineLength = 0;
|
||||
|
||||
for (row = startRow, col = startCol; row < 6 && col < 7; row++, col++)
|
||||
for (row = startRow, col = startCol; row < height && col < width; row++, col++)
|
||||
{
|
||||
CellState cell = fieldArr[row, col];
|
||||
if (cell != currentPlayer)
|
||||
@ -187,28 +170,24 @@ public class FieldModel
|
||||
continue;
|
||||
}
|
||||
lineLength++;
|
||||
if (lineLength == 4)//êîìáèíàöèÿ èç 4 íàøëàñü)
|
||||
if (lineLength == 4)
|
||||
{
|
||||
//int[] indexesRow = new int[4];
|
||||
//int[] indexesColumn = new int[4];
|
||||
for (int i = 0; i < 4; i++)
|
||||
{//èäåì íàçàä è çàïîëíÿåì èíäåêñû
|
||||
{
|
||||
tempIndexesColumn[i] = col - i;
|
||||
tempIndexesRow[i] = row - i;
|
||||
//indexesColumn[i] = col - i;
|
||||
//indexesRow[i] = row - i;
|
||||
}
|
||||
return new CheckResult(true, false, tempIndexesRow, tempIndexesColumn);
|
||||
}
|
||||
}
|
||||
return new CheckResult(false, false);//íå íàøëè êîìáèíàöèþ
|
||||
return new CheckResult(false, false);
|
||||
}
|
||||
|
||||
|
||||
public int getHeight(int colIndex)
|
||||
{
|
||||
//ïåðåáèðàåì ðÿäû äëÿ âûáðàííîé êîëîíêè
|
||||
//èùåì ñàìûé íèæíèé ñëîò â êîëîíêå, êóäà ìîæåì âñòàâèòü ôèøêó
|
||||
for (int row = 5; row >= 0; row--)
|
||||
for (int row = height-1; row >= 0; row--)
|
||||
{
|
||||
if (fieldArr[row, colIndex] == CellState.Empty)
|
||||
{
|
||||
@ -217,98 +196,17 @@ public class FieldModel
|
||||
}
|
||||
return -1;//åñëè íå íàøëè, òî áîëüøå äîáàâèòü íåëüçÿ
|
||||
}
|
||||
|
||||
//Îïòèìèçèðîâàííûå ïðîâåðêè äëÿ áîòà
|
||||
|
||||
/* public bool checkRow(int row, CellState currentPlayer, CellState[,] field)
|
||||
{
|
||||
//Debug.Log("Ðÿä");
|
||||
if (field[row, 3] != currentPlayer)
|
||||
return false;
|
||||
|
||||
int lineLength = 0;
|
||||
for (int i = 0; i < 7; i++)
|
||||
{
|
||||
if (field[row, i] != currentPlayer)
|
||||
{
|
||||
lineLength = 0;
|
||||
continue;
|
||||
}
|
||||
lineLength++;
|
||||
|
||||
if (lineLength == 4)//íàøëè ðÿä èç 4 ôèøåê
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}*/
|
||||
|
||||
|
||||
//ïðîâåðêà íè÷üåé
|
||||
public bool checkDraw()
|
||||
{
|
||||
for (int col = 0; col < 7; col++)
|
||||
for (int col = 0; col < width; col++)
|
||||
{
|
||||
//åñëè åñòü õîòü îäíà íåïóñòàÿ êîëîíêà òî èãðà ïðîäîëæàåòñÿ
|
||||
if (getHeight(col) != -1) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/* //Ãåòòåðû äëÿ ðàçíûõ ðÿäîâ. Âîçâðàùàþò îäíîìåðíûå ìàññèâû
|
||||
CellState[] getRow(int rowIndex)
|
||||
{
|
||||
CellState[] row = new CellState[7];
|
||||
for (int col = 0; col<7; col++)
|
||||
{
|
||||
row[col] = fieldArr[rowIndex, col];
|
||||
}
|
||||
return row;
|
||||
}
|
||||
CellState[] getColumn(int colIndex)
|
||||
{
|
||||
CellState[] col = new CellState[6];
|
||||
for (int row = 0; row < 6; row++)
|
||||
{
|
||||
col[row] = fieldArr[row, colIndex];
|
||||
}
|
||||
return col;
|
||||
}
|
||||
CellState[] getUpDownDiagonal(int row, int col)
|
||||
{
|
||||
int sub = row - col;
|
||||
if (sub <= -4 || sub >= 3) //ïðè òàêîé ðàçíèöå èíäåêñîâ 4 ôèøêè â äèàãîíàëü íå ïîìåùàþòñÿ
|
||||
return new CellState[0];
|
||||
|
||||
//ñ÷èòàåì èíäåêñ íà÷àëà äèàãîíàëè (ëåâî âåðõ)
|
||||
int startRow = Mathf.Clamp(sub, 0, 2);
|
||||
int startCol = Mathf.Abs(Math.Clamp(sub, -3, 0));
|
||||
|
||||
List<CellState> result = new List<CellState>();//çàðàíåå ñëîæíî îáñ÷èòàòü ñêîëüêî â äèàãîíàëè ýëåìåíòîâ, ïîýòîìó ëèñò
|
||||
for (row = startRow, col = startCol; row < height && col < width; row++, col++)
|
||||
{
|
||||
result.Add(fieldArr[row, col]);
|
||||
}
|
||||
result.TrimExcess();//Óáðàòü íåçàïîëíåííûå ïîçèöèè
|
||||
return result.ToArray();
|
||||
}
|
||||
CellState[] getDownUpDiagonal(int row, int col)
|
||||
{
|
||||
int sum = row + col;
|
||||
if (sum <= 2 || sum >= 9) //ïðè òàêîé ñóììå èíäåêñîâ 4 ôèøêè â äèàãîíàëü íå ïîìåùàþòñÿ
|
||||
return new CellState[0];
|
||||
|
||||
//ñ÷èòàåì èíäåêñ íà÷àëà äèàãîíàëè (ëåâî íèç)
|
||||
int startRow = Mathf.Clamp(sum, 0, 5);
|
||||
int startCol = Mathf.Clamp(sum - 5, 0, 5);
|
||||
|
||||
List<CellState> result = new List<CellState>();//çàðàíåå ñëîæíî îáñ÷èòàòü ñêîëüêî â äèàãîíàëè ýëåìåíòîâ, ïîýòîìó ëèñò
|
||||
for (row = startRow, col = startCol; row >=0 && col < width; row--, col++)
|
||||
{
|
||||
result.Add(fieldArr[row, col]);
|
||||
}
|
||||
result.TrimExcess();//Óáðàòü íåçàïîëíåííûå ïîçèöèè
|
||||
return result.ToArray();
|
||||
}*/
|
||||
}
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user