בעיה בהדפסת עלים של עץ משחק ב-JAVA - תכנות - HWzone פורומים
עבור לתוכן
  • צור חשבון

בעיה בהדפסת עלים של עץ משחק ב-JAVA


ניר.

Recommended Posts

קוד הפונקציה:


private void printLeaves(int level, GameTreeNode node)
{
int player=participant;
Point move = node.getMove();
ArrayList<GameTreeNode> children=node.getChildren();
Iterator<GameTreeNode> itChild=null;
if(node!=null)
{
if (children != null)
itChild=children.iterator();

if(level>0)
{
player = level%2==0 ? participant : presentBoard.opposite(participant);
if(move != null)
presentBoard.makeMove(player, move);
}

if(children!=null)
{
while(itChild.hasNext())
{
GameTreeNode child = itChild.next();
printLeaves(level+1, child);
}
}
else
{
System.out.println("Level " + level + ": " + (player==PLAYER ? "whitePlayer" : "blackPlayer") + " at " + node.getMove().x + ", " + node.getMove().y);
}
if(move != null)
presentBoard.unMakeMove(node.getMove());
}
else
{
System.out.println("There Is No Root .");
}
}

הקריאה לפונקציה:


printLeaves(0, root);

הפלט:


Level 2: whitePlayer at 3, 4
Level 2: whitePlayer at 3, 6
Level 2: whitePlayer at 5, 6

Level 2: whitePlayer at 3, 4
Level 2: whitePlayer at 3, 6
Level 2: whitePlayer at 5, 6

Level 2: whitePlayer at 3, 4
Level 2: whitePlayer at 3, 6
Level 2: whitePlayer at 5, 6

שלום רב !!

אני בונה פרויקט בג'אווה עם עץ משחק אשר לאחר בנייתו אני מדפיס את ערכי הנקודות / המיקום של העלים אך כפי שניתן לראות הפונקציה הנ"ל מדפיסה לי את העלים של העץ 3 פעמי במקום פעם אחת...

שאלתי היא מה הבעיה בקוד של הפונקציה הנ"ל אשר מדפיסה לי את העלים של העץ 3 פעמים במקום פעם אחת , איפה הפקודה / קטע הקוד הבעיתי וכיצד מסדרים אותו כך שהוא ידפיס לי את הנתונים הרלוונטיים אך ורק פעם אחת?

הערה: אני הוספתי את השורה הריקה לשם הדגשת החזרה של הפונקציה...

קישור לתוכן
שתף באתרים אחרים

  • תגובות 37
  • נוצר
  • תגובה אחרונה

בסופו של דבר צריך להיות מודפס באמת רק פעם אחת כך שהפלט יהיה באופן הבא בלבד:


Level 2: whitePlayer at 3, 4
Level 2: whitePlayer at 3, 6
Level 2: whitePlayer at 5, 6

אתה יודע איך מסדרים את הפונקציה כך שהבעיה תפתר ?

קישור לתוכן
שתף באתרים אחרים

מי אמר שהבעיה היא בפונקציה הזו ולא באחת מהאחרות שהיא קוראת להם ?

בכל מקרה, יש לך גם טעות בבדיקת האם node הוא null או לא, אתה עושה את זה אחרי שאתה מפעיל עליו כבר פונקציות (שכמובן יזרקו שגיאה אם הוא null לפני כן).

קישור לתוכן
שתף באתרים אחרים

זאת בעיה של תקינות קוד לא בעייה באלגוריתם. אתה אמור לשים את הבדיקה האם זה NULL לפני שאתה מנסה לגשת לאובייקט בצורה כלשהיא.

לגבי הבעיה, קשה לדעת בלי לראות את כל הקוד.

קישור לתוכן
שתף באתרים אחרים

תיקנתי ועדכנתי , הקוד המתוקן הוא:


private void printLeaves(int level, GameTreeNode node)
{
int player=participant;
Iterator<GameTreeNode> itChild=null;
if(node!=null)
{
Point move = node.getMove();
ArrayList<GameTreeNode> children=node.getChildren();
if (children != null)
itChild=children.iterator();
else
return;
if(level>0)
{
player = level%2==0 ? participant : presentBoard.opposite(participant);
if(move != null)
presentBoard.makeMove(player, move);
}

if(children!=null)
{
while(itChild.hasNext())
{
GameTreeNode child = itChild.next();
printLeaves(level+1, child);
}
}
else
{
System.out.println("Level " + level + ": " + (player==PLAYER ? "whitePlayer" : "blackPlayer") + " at " + node.getMove().x + ", " + node.getMove().y);
}
if(move != null)
presentBoard.unMakeMove(node.getMove());
}
else
{
System.out.println("There Is No Root .");
}
}

הפונקציות וקודיהם (כולל הגדרת המשתנים) אשר נעשה בהם שימוש בפונציה הנ"ל הם:


private ArrayList<GameTreeNode> children;
public ArrayList<GameTreeNode> getChildren()
{
return children;
}


private Point move;
public Point getMove()
{
return move;
}


public int opposite (int participant)
{
if (participant==PERSON)
return PLAYER;
else
return PERSON;
}


public void makeMove(int participant,Point move)
{
previousMove=move;
int op=opposite(participant);
int row=move.x,col=move.y;
field[row][col]=participant;
//Consider all spokes around this move;
for (int spoke=0;spoke<8;spoke++)
{
int colStep=spokes[spoke].y;
int rowStep=spokes[spoke].x;
//Do we flip this spoke? only if it starts with an oppenent's stone...
if (field[row+rowStep][col+colStep]==op)
{
//...and has more oppenents stons...
int colInc=col+colStep+colStep;
int rowInc=row+rowStep+rowStep;
while(field[rowInc][colInc]==op)
{
colInc+=colStep;
rowInc+=rowStep;
}
//and starts with our stone.
if (field[rowInc][colInc]==participant)
{
//Flip the contiuous line of oppenents stones on this spoke.
colInc=col+colStep;
rowInc=row+rowStep;
while(field[rowInc][colInc]==op)
{
field[rowInc][colInc]=participant;
colInc+=colStep;
rowInc+=rowStep;
}
}
}
}
computePossibleMoves(participant);
computePossibleMoves(op);
}


public void unMakeMove(Point move)
{
field[move.x][move.y]=VACANT;
}

עדיין יש לי את הבעיה...

האם צירוך מימוש הפונקציות עוזר לך ?

כעת אתה יכול למקד אותי בדיוק היכן נמצאת הבעיה ?

מה יש לעשות כדי לפתור את הבעיה ?

האם יש עוד מקומות אשר יכולים לגרום לבעיה ?

קישור לתוכן
שתף באתרים אחרים

אתה בטוח שהוא עדיין מדפיס אותו דבר?

כי לוגית הוא לעולם לא יכול להגיע לשורת ההדפסה ???

מעבר לזה לא הבנתי למה בפונקציה שמטרתה להדפיס עץ קוראים לפונקציות שמשנות את לוח המשחק (makeMove,unMakeMove), מה גם שהן לא הופכיות אחת לשניה מה שיצור לך בלגאן לא קטן בלוח אחרי שהקריאה לפונקציה תגמר.

קישור לתוכן
שתף באתרים אחרים

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.


×
  • צור חדש...