עבור לתוכן

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

Featured Replies

פורסם

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


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
  • צפיות 3.9k
  • נוצר
  • תגובה אחרונה
פורסם

יש שם הדפסה של שורה ריקה. תבדוק איפה הדפסת אותה.

פורסם
  • מחבר

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

בהדפסה המקורית אלו שורות רציפות בלי שורות רווח ..

סליחה על אי ההבנה...

פורסם

ואיך העץ שלך באמת נראה? מה צריך להיות מודפס באמת?

פורסם
  • מחבר

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


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

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

פורסם

אני לא רואה בעיה מיוחדת בקוד. תוודא שיש לך רק 3 עלים.

יכול להיות שלnode שלך יש 3 בנים, ולכל אחד מהם 3 בנים?

פורסם
  • מחבר

יש רק 3 עלים משום שאתה רואה שהוא מדפיס את אותם 3 ערכים שוב ושוב 3 פעמים...

פורסם

ניסית לדבג את זה עם DEBUGGER ולראות מתי אתה מדפיס?

פורסם
  • מחבר

כן ובכל זאת לא הצלחתי לאתר את הבעיה ולפתור אותה..

פורסם
  • מחבר

יש מישהו נוסף הבין מה הבעיה ויודע איך פותרים אותה.....

פורסם

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

בכל מקרה, יש לך גם טעות בבדיקת האם 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), מה גם שהן לא הופכיות אחת לשניה מה שיצור לך בלגאן לא קטן בלוח אחרי שהקריאה לפונקציה תגמר.

ארכיון

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

דיונים חדשים