Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > IT форум > Помощь студентам
Регистрация

Восстановить пароль

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 22.12.2011, 18:28   #1
STERVA
 
Регистрация: 04.03.2010
Сообщений: 4
Печаль Перевод из С# на Java

Добрый вечер.Извините конечно, вы можете перевести с одного языка на другой. Выручите, если не трудно.
Код:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{public partial class Form1 : Form
{
const int nw = 4, nh = 4; // 4х4 - размер игрового поля
System.Drawing.Graphics g; // графическая поверхность формы
Bitmap pics;
int cw, ch;
 int[,] field = new int[nw, nh];
 int ex, ey;
Boolean showNumbers = false; 
 public Form1()
{
InitializeComponent();
try
{
 pics = new Bitmap("волчик.bmp");
 }
catch (Exception exc)
 {
 MessageBox.Show("Файл 'волчик.bmp' не найден.\n",
 "Собери картинку",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
 this.Close();
 return;}
cw = (int)(pics.Width / nw);
ch = (int)(pics.Height / nh);
this.ClientSize =new System.Drawing.Size(cw * nw + 1, ch * nh + 1 + menuStrip1.Height);
 g = this.CreateGraphics();
this.newGame();}
 private void newGame()
{
for (int j = 0; j < nh; j++)
for (int i = 0; i < nw; i++)
field[i, j] = j * nw + i + 1;
 field[nw - 1, nh - 1] = 0;
            ex = nw - 1; ey = nh - 1;
this.mixer();      
            this.drawField();    
        }
private void mixer()
        {
           int d;   
           int x, y;
Random rnd = new Random();
            for (int i = 0; i < nw * nh * 10; i++)
        {
                x = ex;
                y = ey;

                d = rnd.Next(4);
                switch (d)
                {
                    case 0: if (x > 0) x--; break;
                    case 1: if (x < nw - 1) x++; break;
                    case 2: if (y > 0) y--; break;
                    case 3: if (y < nh - 1) y++; break;
                }
field[ex, ey] = field[x, y];
                field[x, y] = 0;
ex = x; ey = y;
            }
        }
private void drawField()
        {
       for (int i = 0; i < nw; i++)
                for (int j = 0; j < nh; j++)
                {
                    if (field[i, j] != 0)
                g.DrawImage(pics,
                            new Rectangle(i * cw, j * ch + menuStrip1.Height, cw, ch),
                            new Rectangle(
                                ((field[i, j] - 1) % nw) * cw,
                                ((field[i, j] - 1) / nw) * ch,
                                cw, ch),
                            GraphicsUnit.Pixel);
                    else
                        // выводим пустую фишку
                        g.FillRectangle(SystemBrushes.Control,
                            i * cw, j * ch + menuStrip1.Height, cw, ch);
g.DrawRectangle(Pens.Black,i * cw, j * ch + menuStrip1.Height, cw, ch);
if ((showNumbers) && field[i, j] != 0)
g.DrawString(Convert.ToString(field[i, j]),new Font("Tahoma", 10, FontStyle.Bold),Brushes.Black, i * cw + 5, j * ch + 5 + menuStrip1.Height);}}
private Boolean finish()
{
int i = 0;
int j = 0;
int c;       
for (c = 1; c < nw * nh; c++)
{
 if (field[i, j] != c) return false;
 if (i < nw - 1) i++;
else { i = 0; j++; }
}
return true;
}
private void move(int cx, int cy)
{
if (!(((Math.Abs(cx - ex) == 1) && (cy - ey == 0)) ||((Math.Abs(cy - ey) == 1) && (cx - ex == 0))))
return;
field[ex, ey] = field[cx, cy];
field[cx, cy] = 0;
ex = cx; ey = cy;
this.drawField();
if (this.finish())
{
field[nw - 1, nh - 1] = nh * nw;
this.drawField();
 if (MessageBox.Show("Поздравляю! Вы справились с поставленной задачей!\n" +"Еще раз?", "Собери картинку", MessageBoxButtons.YesNo,MessageBoxIcon.Question)== System.Windows.Forms.DialogResult.No)
this.Close();
else this.newGame();}}
private void Form1_Paint(object sender, PaintEventArgs e)
{
drawField();}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
move( e.X/cw, (e.Y-menuStrip1.Height)/ch);
}
private void новаяИграToolStripMenuItem_Click(object sender, EventArgs e)
{ newGame();}
private void оПрограммеToolStripMenuItem_Click(object sender, EventArgs e){
Form2 f = new Form2();
f.ShowDialog();
}}}
STERVA вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц

Опции темы Поиск в этой теме
Поиск в этой теме:

Расширенный поиск


Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
перевод программы с Pascal на Java christa Помощь студентам 1 23.09.2011 19:53
Перевод String в int [JAVA] J.Bond Помощь студентам 2 01.03.2011 10:29
Перевод на Java Ksuxa Общие вопросы по Java, Java SE, Kotlin 2 06.09.2010 01:01
Перевод программы с delphi на Java zlobagi Фриланс 1 15.05.2010 17:54
перевод с Java на C Lys Помощь студентам 2 10.12.2009 01:50