Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- using System.Windows.Forms;
- using System.Threading;
- using System.IO;
- namespace puzzle_game
- {
- public partial class Form1 : Form
- {
- Button[,] btns;
- int[] blank = new int[2];
- Image[] cropped = new Image[9];
- Point[] btnlocation;//pre determined locations for buttons, for resetting
- Boolean isPlaying = false;
- Image splitdimg;
- String splitdImgfilename = "";
- Bitmap btndefault = new Bitmap("res/button.png");
- Bitmap blankbtn = new Bitmap("res/blank.png");
- public Form1()// initialises the btns, bntlocation and calls movetiles()
- {
- InitializeComponent();
- btns = new Button[3, 3] { { btn1, btn2, btn3 }, { btn4, btn5, btn6 }, { btn7, btn8, btn9 } };
- btnlocation = new Point[9]{btn1.Location,btn2.Location,btn3.Location,btn4.Location,btn5.Location,btn6.Location,btn7.Location,btn8.Location,btn9.Location};
- movetiles();
- }
- private void movetiles()//randomly moves tiles to simulate 500 mouse clicks on random adjacent(to the blank) tiles, ensures winnability
- { Random rdm = new Random();
- int tmp=0;
- isPlaying = false;
- if (splitdimg != null)
- {LoadImage(splitdimg);}
- reset();
- for (int l = 0; l < 500; l++)//simulating the 500 clicks
- { tmp = rdm.Next(0, 5);
- try
- {
- switch (tmp)//moving the blank
- {
- case 1:
- check(blank[0] + 1, blank[1]);
- break;
- case 2:
- check(blank[0] - 1, blank[1]);
- break;
- case 3:
- check(blank[0], blank[1]-1);
- break;
- case 4:
- check(blank[0], blank[1]+1);
- break;
- }
- }catch (Exception){}
- }
- lblwin.Text = "";
- isPlaying = true;
- }
- private void swap(int chk1, int chk2)//simply changes the text value of one button to another, specifically the button pressed to the blank and vice versa
- { btns[blank[0], blank[1]].Text = btns[chk1, chk2].Text;//changes blank to the button pressed
- btns[blank[0], blank[1]].Tag = btns[chk1, chk2].Tag;
- if (splitdimg == null){//swapping background image/setting to blank
- btns[blank[0], blank[1]].BackgroundImage = btndefault;
- }else{
- btns[blank[0], blank[1]].BackgroundImage = btns[chk1, chk2].BackgroundImage;
- }
- btns[chk1, chk2].BackgroundImage = blankbtn;
- blank[0] = chk1;//changes button pressed to the blank
- blank[1] = chk2;
- btns[chk1, chk2].Text = "";//the button pressed becomes the blank
- btns[chk1, chk2].Tag = "";
- }
- private bool wincheck()//checks if the game is won
- {
- bool iswin = false;
- int winincr = 1;
- for (int i = 0; i <= 2; i++)//the first dim of the array
- {
- for (int n = 0; n <= 2; n++)//second dim
- {
- if (!(winincr >= 9))//check for last button, basically if it gets to the 9th one it should be blank, and you should win.
- {
- if (btns[i, n].Tag != winincr.ToString())//because the win is 1,2,3,4 etc the next button should correspond to a incrament of 1 each time,
- { //so if it is not then you loose and the loop breaks
- iswin = false;
- i = 10;
- break;
- }else{iswin = true;}//if they are the same each time then the method will return true, meaning a win
- }
- winincr++;
- }
- }
- return iswin;
- }
- private void check(int chk1,int chk2)//checks to see if the blank space is next to the button press and also calls wincheck
- {//since the parameters are the buttons array coords:
- if (((blank[0] + 1) == chk1) && (blank[1] == chk2)){//checks to see if the blank is to the direct bottom of the button pressed
- swap(chk1, chk2); }
- else if (((blank[0] - 1) == chk1) && (blank[1] == chk2)){//to the top
- swap(chk1, chk2); }
- else if ((blank[0] == chk1) && ((blank[1] + 1) == chk2)){//to the right
- swap(chk1, chk2);}
- else if ((blank[0] == chk1) && ((blank[1] - 1) == chk2)) {//and to the left of the blank
- swap(chk1, chk2); }//if the button pressed is next to the blank then they are swapped
- if (wincheck() && isPlaying){//checks win each button press because when else would you do it
- onWin();
- }else{
- lblwin.Text = "";}
- }
- private void onWin()//what happens when the game is won
- {
- lblwin.Text = "YOU ARE WINNER, HAHAHA";
- for (int i = 0; i <= 2; i++)
- {
- for (int l = 0; l <= 2; l++)//disables all the tiles cause the game is won
- {btns[i, l].Enabled = false;}
- }
- if (splitdimg != null)
- btns[2, 2].BackgroundImage = cropped[8];//makes it so full image appears on win, if image is loaded
- }
- private void btn1_Click(object sender, EventArgs e){ check(0, 0); }//buttons simply send their array coordinates to the check function
- private void btn2_Click(object sender, EventArgs e){ check(0, 1); }
- private void btn3_Click(object sender, EventArgs e){ check(0, 2); }
- private void btn4_Click(object sender, EventArgs e){ check(1, 0); }
- private void btn5_Click(object sender, EventArgs e){ check(1, 1); }
- private void btn6_Click(object sender, EventArgs e){ check(1, 2); }
- private void btn7_Click(object sender, EventArgs e){ check(2, 0); }
- private void btn8_Click(object sender, EventArgs e){ check(2, 1); }
- private void btn9_Click(object sender, EventArgs e){ check(2, 2); }
- private void btnshuffle_Click(object sender, EventArgs e){ movetiles(); }//allows player to reset/change game if they're a casual
- private void reset()//resets buttons to a win or default position
- { int iter=1;
- for (int i = 0; i <= 2; i++)//sets the buttons to default for simulating, especially for shuffle button(sets buttons 1-8 then blank)
- {
- for (int n = 0; n <= 2; n++)
- {
- btns[i, n].Enabled = true;
- if (iter == 9)
- {
- btns[i, n].Text = "";
- btns[i, n].Tag = "";
- blank[0] = i;
- blank[1] = n;
- }else{
- if (splitdimg == null)
- {
- btns[i, n].Text = iter.ToString();
- }
- else
- {
- btns[i, n].Text = "";
- }
- btns[i, n].Tag = iter.ToString();
- iter++;
- }
- }
- }
- }
- private void saveGame()// Saves the game to a binary file
- {
- SaveFileDialog savedialg = new SaveFileDialog();// created dialog for users to create a specific file
- savedialg.Filter = "Binary Files (*.bin)|*.bin"; // setting filters/ asthetics
- savedialg.Title = "Create/select file to save to";
- savedialg.CreatePrompt = true;
- savedialg.ShowDialog();
- Stream save = savedialg.OpenFile();
- BinaryWriter writr = new BinaryWriter(save);// opens a new binarywriter using the newly created file
- if (splitdimg != null)//if there is a picture loaded then the filename is saved too
- {
- writr.Write(splitdImgfilename);
- }
- else
- {
- writr.Write("none");
- }
- for (int i = 0; i <= 2; i++)
- {
- for (int l = 0; l <= 2; l++)
- {
- if (btns[i, l].Text == "")// writing the buttons in order
- {
- writr.Write(9);
- }
- else
- {
- writr.Write(Int32.Parse(btns[i, l].Text));
- }
- }
- }
- savedialg.Dispose();
- save.Close();
- writr.Close();
- }
- private void loadGame()// loads the game from a binary file
- {
- OpenFileDialog dialg = new OpenFileDialog();
- dialg.Filter = "Binary Files(*.bin)|*.bin";
- dialg.ShowDialog();
- if (dialg.FileName == null || dialg.FileName == "")
- {
- return;
- }
- Stream load = dialg.OpenFile();
- BinaryReader bReadr = new BinaryReader(load);//creates a new binaryreader using the user specified file
- Int32 tmpint = 0;
- try
- {
- splitdImgfilename = bReadr.ReadString();
- if (splitdImgfilename != "none")
- {
- splitdimg = new Bitmap(splitdImgfilename);// tries to load a saved image
- }
- Loadcrops(splitdimg);
- }
- catch (Exception)
- {
- }
- for (int i = 0; i <= 2; i++)
- {
- for (int l = 0; l <= 2; l++)
- {
- tmpint = bReadr.ReadInt32();//loads the next value into a temp
- if (tmpint == 9)// sets the buttons to the loaded order
- {
- blank[0] = i;
- blank[1] = l;
- btns[i, l].BackgroundImage = blankbtn;
- btns[i, l].Text = "";
- }
- else
- {
- if (splitdimg != null)
- {
- btns[i, l].BackgroundImage = cropped[tmpint - 1];
- btns[i, l].Text = "";
- }
- else
- {
- btns[i, l].BackgroundImage = btndefault;
- btns[i, l].Text = tmpint.ToString();
- }
- }
- }
- }
- load.Close();
- bReadr.Close();
- }
- private static Image cropImage(Image img, Rectangle cropArea)//crops specified image using specified rect, returns cropped image
- { Bitmap bmpCrop =null;
- try
- { Bitmap bmpImage = new Bitmap(img);
- bmpCrop = bmpImage.Clone(cropArea, bmpImage.PixelFormat);//creates a clone of the area essentially cropping it
- }
- catch (Exception ex)
- {MessageBox.Show(ex.Message + " rect h" + cropArea.Height + " w" + cropArea.Width + " x" +cropArea.X + " y" + cropArea.Y ); }
- GC.Collect();
- return (Image)(bmpCrop);
- }
- private void Loadcrops(Image img)//crops a picture using the cropimage function and puts them into an array
- {
- int hres = img.Width / 3, vres = img.Height / 3;
- int iter = 0;
- for (int i = 0; i <= 2; i++)
- {
- for (int l = 0;l <= 2; l++)
- {
- Rectangle rec = new Rectangle(hres * l, vres * i, hres, vres);
- cropped[iter] = cropImage(img, rec);
- iter++;
- btns[i, l].Top = (124 * i) + 100;
- btns[i, l].Left = (124 * l) + 100;
- btns[i, l].Size = new Size(124, 124);
- }
- }
- }
- private void LoadImage(Image img)//Loads the cropped array into the buttons
- {
- splitdimg = img;
- int iter=0;
- Loadcrops(splitdimg);
- for (int i = 0; i <= 2; i++)
- {
- for (int l = 0; l <= 2; l++)
- {
- if (!(i == 2 && l == 2))
- {
- btns[i, l].BackgroundImage = cropped[iter];
- }
- iter++;
- btns[i, l].Top = (124* i) + 100;
- btns[i, l].Left = (124 * l) + 100;
- btns[i, l].Size = new Size(124, 124);
- }
- }
- }
- private void btnload_Click(object sender, EventArgs e)//creates a dialog for a user to load an image, then loads/shuffles tiles
- { OpenFileDialog dialg = new OpenFileDialog();
- dialg.Title="Open Image";
- dialg.Filter = "Image Files (*.bmp*.jpg; *.jpeg; *.bmp; *.png)|*.jpg; *.jpeg; *.bmp; *.png";
- dialg.ShowDialog();
- if (dialg.FileName != ""){
- splitdimg = new Bitmap(dialg.FileName);
- splitdImgfilename = dialg.FileName;
- for (int i = 0; i <= 2; i++)
- {
- for (int n = 0; n <= 2; n++)
- {
- btns[i, n].Text = "";
- }
- }
- movetiles();}//load/shuffle
- }
- private void btnreset_Click(object sender, EventArgs e)//resets the game back to default, and shuffles
- { int iter=0;
- splitdimg = null;
- for (int i = 0; i <= 2; i++)
- {
- for (int l = 0; l <= 2; l++)//reseting the game
- {
- btns[i,l].Location = btnlocation[iter];//pre-determined locations being loaded
- btns[i, l].Size = new Size(96, 96);
- btns[i, l].BackgroundImage = btndefault;
- iter++;
- }
- }
- movetiles();
- }
- private void btnsave_Click(object sender, EventArgs e){saveGame();}
- private void btnloadgame_Click(object sender, EventArgs e){ loadGame(); }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement