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

Real #1

Open
wants to merge 7 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
88 changes: 79 additions & 9 deletions TwoZeroFourEightModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,41 @@ public TwoZeroFourEightModel(int size)
}
}
rand = new Random();
board = Random(board);
NotifyAll();
// initialize board
HandleChanges();
}

public int[,] GetBoard()
{
return board;
}

private int[,] Random(int[,] input)
public int Getscore()
{
int score1 = 0;
foreach(int num in board)
{
if (num != 0)
{
score1 += num;
}
}
return score1;
}

private void AddRandomSlot()
{
while (true)
{
int x = rand.Next(boardSize);
int y = rand.Next(boardSize);
if (input[x, y] == 0)
if (board[x, y] == 0)
{
input[x, y] = 2;
break;
board[x, y] = 2;
return;
}

}
return input;
}

// Perform shift and merge to the left of the given array.
Expand Down Expand Up @@ -91,13 +104,13 @@ protected bool ShiftAndMerge(int[] buffer)
return changed;
}

protected void HandleChanges(bool changed)
protected void HandleChanges(bool changed = true)
{
// if the board has changed, add a new number
// and notify all views
if (changed)
{
board = Random(board);
AddRandomSlot();
NotifyAll();
}
}
Expand Down Expand Up @@ -193,5 +206,62 @@ public void PerformLeft()
}
HandleChanges(changed);
}

public bool gameoverBox()
{
for(int i=0;i< boardSize; i++)
{
for(int j = 0; j < boardSize; j++)
{
if (board[i, j] == 0)
{
return false;
}
}
}

int tmp;
for(int i = 0; i < boardSize; i++)
{
for(int j = 0; j < boardSize; j++)
{
tmp = board[i, j];
if (i - 1 >= 0)
{
if(tmp==board[i-1,j])
{
return false;
}
}
if (i + 1 <= 3)
{
if (tmp == board[i+1, j])
{
return false;
}
}
if (j - 1 >= 0)
{
if (tmp == board[i,j-1])
{
return false;
}
}
if (j + 1 >= 0)
{
if (tmp == board[i, j+1])
{
return false;
}
}



}

}
return true;

}
}
}
29 changes: 29 additions & 0 deletions TwoZeroFourEightView.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 54 additions & 20 deletions TwoZeroFourEightView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public partial class TwoZeroFourEightView : Form, View
{
Model model;
Controller controller;

public TwoZeroFourEightView()
{
InitializeComponent();
Expand All @@ -27,15 +27,19 @@ public TwoZeroFourEightView()

public void Notify(Model m)
{
UpdateBoard(((TwoZeroFourEightModel) m).GetBoard());
UpdateBoard(((TwoZeroFourEightModel)m).GetBoard());
scorebox.Text = ((TwoZeroFourEightModel)m).Getscore().ToString();

}

private void UpdateTile(Label l, int i)
{
if (i != 0)
{
l.Text = Convert.ToString(i);
} else {
}
else
{
l.Text = "";
}
switch (i)
Expand All @@ -59,22 +63,22 @@ private void UpdateTile(Label l, int i)
}
private void UpdateBoard(int[,] board)
{
UpdateTile(lbl00,board[0, 0]);
UpdateTile(lbl01,board[0, 1]);
UpdateTile(lbl02,board[0, 2]);
UpdateTile(lbl03,board[0, 3]);
UpdateTile(lbl10,board[1, 0]);
UpdateTile(lbl11,board[1, 1]);
UpdateTile(lbl12,board[1, 2]);
UpdateTile(lbl13,board[1, 3]);
UpdateTile(lbl20,board[2, 0]);
UpdateTile(lbl21,board[2, 1]);
UpdateTile(lbl22,board[2, 2]);
UpdateTile(lbl23,board[2, 3]);
UpdateTile(lbl30,board[3, 0]);
UpdateTile(lbl31,board[3, 1]);
UpdateTile(lbl32,board[3, 2]);
UpdateTile(lbl33,board[3, 3]);
UpdateTile(lbl00, board[0, 0]);
UpdateTile(lbl01, board[0, 1]);
UpdateTile(lbl02, board[0, 2]);
UpdateTile(lbl03, board[0, 3]);
UpdateTile(lbl10, board[1, 0]);
UpdateTile(lbl11, board[1, 1]);
UpdateTile(lbl12, board[1, 2]);
UpdateTile(lbl13, board[1, 3]);
UpdateTile(lbl20, board[2, 0]);
UpdateTile(lbl21, board[2, 1]);
UpdateTile(lbl22, board[2, 2]);
UpdateTile(lbl23, board[2, 3]);
UpdateTile(lbl30, board[3, 0]);
UpdateTile(lbl31, board[3, 1]);
UpdateTile(lbl32, board[3, 2]);
UpdateTile(lbl33, board[3, 3]);
}

private void btnLeft_Click(object sender, EventArgs e)
Expand All @@ -97,5 +101,35 @@ private void btnDown_Click(object sender, EventArgs e)
controller.ActionPerformed(TwoZeroFourEightController.DOWN);
}

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
// keydata equals Keys.Up (if up arrow is pressed)
if(keyData==Keys.Up||keyData==Keys.Down||keyData==Keys.Left||keyData==Keys.Right){

switch(keyData){
case Keys.Up :
controller.ActionPerformed(TwoZeroFourEightController.UP);
break;
case Keys.Down :
controller.ActionPerformed(TwoZeroFourEightController.DOWN);
break;
case Keys.Left :
controller.ActionPerformed(TwoZeroFourEightController.LEFT);
break;
case Keys.Right :
controller.ActionPerformed(TwoZeroFourEightController.RIGHT);
break;
}
return true;
}
else{
return false;
}

}


// return base.ProcessCmdKey(ref msg, keyData);
}
}
}