Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

600610802 Lab5 #82

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions TwoZeroFourEightModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class TwoZeroFourEightModel : Model
protected int[,] board;
protected Random rand;
protected int[] range;
int s1 = 0;

public TwoZeroFourEightModel() : this(4)
{
Expand Down Expand Up @@ -69,6 +70,8 @@ protected bool ShiftAndMerge(int[] buffer)
{
// merge
buffer[pos - 1] *= 2;
//GetScore(buffer[pos-1]);

buffer[k] = 0;
lastMergedSlot = pos - 1;
changed = true;
Expand Down Expand Up @@ -192,5 +195,101 @@ public void PerformLeft()
}
HandleChanges(changed);
}

public bool GameFull()
{
bool isGameOver = false;
int count = 0;
int tmp_count = 0;
//Check left
foreach (int i in range)
{
int[] buffer = new int[boardSize];
foreach (int j in range)
{
buffer[j] = board[i, j];
}
if (!ShiftAndMerge(buffer))
{
tmp_count++;
}
if (tmp_count == 4)
{
count++;
}
}
tmp_count = 0;
//Check rigth
foreach (int i in range)
{
int[] buffer = new int[boardSize];
foreach (int j in range)
{
buffer[boardSize - j - 1] = board[i, j];
}
if (!ShiftAndMerge(buffer))
{
tmp_count++;
}
if (tmp_count == 4)
{
count++;
}
}
tmp_count = 0;
//Check up
foreach (int i in range)
{
int[] buffer = new int[boardSize];
foreach (int j in range)
{
buffer[j] = board[j, i];
}
if (!ShiftAndMerge(buffer))
{
tmp_count++;
}
if (tmp_count == 4)
{
count++;
}
}
//Check down
tmp_count = 0;
foreach (int i in range)
{
int[] buffer = new int[boardSize];
foreach (int j in range)
{
buffer[boardSize - j - 1] = board[j, i];
}
if (!ShiftAndMerge(buffer))
{
tmp_count++;
}
if (tmp_count == 4)
{
count++;
}
}
if (count == 4)
{
isGameOver = true;
}
return isGameOver;
}
public int GetScore()
{
int score = 0;
for (int i = 0; i < boardSize; i++)
{
for (int j = 0; j < boardSize; j++)
{
score += board[i, j];
}
}
return score;
}

}
}
Loading