المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : c#&java


صعب المنال
12-18-2007, 12:35 PM
السلام عليكم
من فترا خلصت برمجة لعبة صغيرة بالجافا سميتها لعبة حرب السفن
عبارة عن سطر من الخلاية تتتوزع فيها السفينة على ثلاث اسطر يقوم المستخدم بتخمين مكان السفينة اذا اصاب الثلاث خلاية بالسفينة دمر السفينة المهم ما رح طول شرح للخوارزمية لانو رح اعطيكون اياها بالمقفات المرفقة
السؤال او سبب المشاركة هوا انو انا حولت الكود من الجافا لل c# بس للاسف طلع عندي خطا
بضن سببو oop بس انا متاكد انو لازم يكون هيك
على كل حاال رح حط الكود باللغتين
java واللعبة جاهزة فيه import java.io.*;
public class SimpleDotCom2{
int [] locationCells;
int numofhits=0;
public void setLocationCells(int[]locs){
locationCells=locs;

}
public String checkyourself(String stringGuess){
int guess=Integer.parseInt(stringGuess);
String result="miss";
for(int i=0;i<locationCells.length;i++)
{
if(guess==locationCells[i])
{
result="hit";
numofhits++;
break;

}

}
if(numofhits==locationCells.length){
result="kill";
}
System.out.println(result);
return result;
}
public static void main(String [] args){
int numofguesses=0;//conter whit plyer
SimpleDotCom2 dot=new SimpleDotCom2();
Gamehelper helper=new Gamehelper();
int randomnum=(int)(Math.random()*5);

int [] locationCells={randomnum,randomnum+1,randomnum+2};

dot.setLocationCells(locationCells);
boolean isAlive=true;
while(isAlive==true){
String guess=helper.getuserinput("enter a number");
String result=dot.checkyourself(guess);
numofguesses++;
if(result.equals("kill")){

isAlive=false;
System.out.println(numofguesses+"guesses");
}}


}

public static class Gamehelper{
public String getuserinput(String prompt)
{
String inputline=null;
System.out.print(prompt+" ");
try{
BufferedReader is =new BufferedReader(new InputStreamReader(System.in));
inputline=is.readLine();
if(inputline.length()==0)
return null;

}catch(IOException e){
System.out.println("ioexception:"+e);


}return inputline;
}
}
}


والسي شوربا ورح تشوفو الخطأ بتمرير برامترات عند استدعاء تابع من اوبجكت الصف الرئيسي
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication14
{
class Program
{

public class SimpleDotCom2{
int [] locationCells;
int numofhits=0;
public void setLocationCells(int[]locs){
locationCells=locs;

}
public String checkyourself(String stringGuess){

int guess = Convert.ToInt16(stringGuess);
String result="miss";
for(int i=0;i<locationCells.Length;i++)
{
if(guess==locationCells[i])
{
result="hit";
numofhits++;
break;

}

}
if(numofhits==locationCells.Length)
{
result="kill";
}
Console.WriteLine(result);
return result;
}
public static void main(String [] args){
int numofguesses=0;//conter whit plyer
SimpleDotCom2 dot=new SimpleDotCom2();
Gamehelper helper=new Gamehelper();




dot.setLocationCells(locationCells);
bool isAlive=true;
while(isAlive==true){
String guess=helper.getuserinput("enter a number");
String result=dot.checkyourself(guess);
numofguesses++;
if(result.Equals("kill")){

isAlive=false;
Console.WriteLine(numofguesses+"guesses");
}}


}

public class Gamehelper{
public String getuserinput(String prompt)
{
String inputline=null;
Console.WriteLine(prompt+" ");

inputline= Console.ReadLine();

if (inputline.Length == 0)
return null;

return inputline;
}
}
}
}
}



بس يلي يقدر يفسرلي الخطأ بكون مشكور لألوووو



خوارزمية اللعبة بالمرفقات

Bl@ck Angel
12-23-2007, 01:03 PM
السلام عليكم .
أنا قرءة الخوارزمية وفهمة شوي من اللعبة ..
بس منشان الخطأ في الكود هو بسبب المتحول الممرر للتابع لأنه غير معرف الذي هو مصفوفة أعداد صحيحة تمثل موقع خانات السفينة.
وفي خطأ تاني هو كتابة كلمة main أول حرف صغير يجب أن يكون كبير Main . .
و لاحظة أنو في مشكلة في الخوارزمية أنو لازم يكون موقع السفينة عشوائي.
من شان تشتغل اللعبة أكتب في بداية تابع الـ Main التالي: int[] locationCells ={ 3, 4, 5 };

Golden man
12-24-2007, 12:49 AM
صديقي مثل ما قال Bl@ck Angel و الكود بيصير :


using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication14
{
class Program
{
public class SimpleDotCom2
{
int[] locationCells;
int numofhits = 0;
public void setLocationCells(int[] locs)
{
locationCells = locs;
}
public String checkyourself(String stringGuess)
{
int guess = Convert.ToInt16(stringGuess);
String result = "miss";
for (int i = 0; i < locationCells.Length; i++)
{
if (guess == locationCells[i])
{
result = "hit";
numofhits++;
break;
}
}
if (numofhits == locationCells.Length)
{
result = "kill";
}
Console.WriteLine(result);
return result;
}
public static void Main(String[] args)
{
int numofguesses = 0;//conter whit plyer
SimpleDotCom2 dot = new SimpleDotCom2();
Gamehelper helper = new Gamehelper();
Random R = new Random();
int randomnum = R.Next() % 5;
int[] locationCells ={ randomnum, randomnum + 1, randomnum + 2 };
dot.setLocationCells(locationCells);
bool isAlive = true;
while (isAlive == true)
{
String guess = helper.getuserinput("enter a number");
String result = dot.checkyourself(guess);
numofguesses++;
if (result.Equals("kill"))
{
isAlive = false;
Console.WriteLine(numofguesses + "guesses");
}
}

}
public class Gamehelper
{
public String getuserinput(String prompt)
{
String inputline = null;
Console.WriteLine(prompt + " ");
inputline = Console.ReadLine();
if (inputline.Length == 0)
return null;
return inputline;
}
}
}
}
}

صعب المنال
12-24-2007, 06:58 AM
مشكورين وصلت الفكرا