Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Nyp Override - Overload
package net.emirozturk;
import java.util.ArrayList;
public class Board {
private ArrayList<Tas> tasListesi;
private int x;
private int y;
Board(int en,int boy){
tasListesi = new ArrayList<Tas>();
x = en;
y = boy;
}
Board(int boyut){
tasListesi = new ArrayList<Tas>();
x = boyut;
y = boyut;
}
void tasEkle(Tas t){
tasListesi.add(t);
}
Tas tasGetir(int x,int y){
for(Tas t:tasListesi)
if(t.koordinatKontrolEt(x,y))
return t;
return null;
}
void sonrakiAdimiHesapla(){
for(var t:tasListesi)
t.ilerle();
}
String cizdir(){
String output = "";
for(int i=0;i<x;i++){
for(int j=0;j<y;j++){
Tas t = tasGetir(i,j);
if(t!=null)
output += t.getSekil() + " ";
else
output += ". ";
}
output += "\n";
}
output+="=================================\n";
return output;
}
}
package net.emirozturk;
public class CaprazTas extends Tas {
CaprazTas(int x, int y) {
super(x, y);
sekil = 'C';
}
@Override
void ilerle(){
x+=2;
y+=2;
}
}
package net.emirozturk;
public class DikeyTas extends Tas{
DikeyTas(int x, int y) {
super(x, y);
sekil = 'D';
}
@Override
void ilerle(){
x++;
}
}
package net.emirozturk;
public class Main {
public static void main(String[] args){
Board b = new Board(8);
b.tasEkle(new DikeyTas(0,0));
b.tasEkle(new OzelTas(1,0));
b.tasEkle(new CaprazTas(2,0));
System.out.println(b.cizdir());
b.sonrakiAdimiHesapla();
System.out.println(b.cizdir());
b.sonrakiAdimiHesapla();
System.out.println(b.cizdir());
}
}
package net.emirozturk;
public class OzelTas extends Tas{
OzelTas(int x,int y){
super(x,y);
sekil = 'O';
}
@Override
void ilerle(){
y+=2;
}
}
package net.emirozturk;
public abstract class Tas {
protected char sekil;
protected int x;
protected int y;
Tas(int x,int y){
this.x = x;
this.y = y;
sekil = 'T';
}
char getSekil(){
return sekil;
}
boolean koordinatKontrolEt(int x,int y){
return x == this.x && y == this.y;
}
void ilerle(){
y++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment