简体   繁体   中英

Passing variable locations (as integers) from a jframe (as main class) to another class for manipulation

I have a programming professor that would like us to write a Tic Tac Toe game a specific way. He wants us to have the "board" class and have specific mutators within to deal with the data and keep as much info in that class as possible. Specifically, He would like us to have a few methods where the only variable passed is an integer that has to do with the location of a variable. for example:

public char getMarkAt(int location) //would return the mark at the given location.

Would I be able to have an instantiation of my jframe within this class, and have another class which handled all other work? For example: Have a jframe which was simply my nine boxes and a character array (let's say charArray for simplicity) full of a random character (We'll say 0 for the time being) and then get and set marks from the class by creating an instance of the class JFrame1 (named Picture) doing something like

JFrame1 Picture = new JFrame1;    
Picture.charArray[location] = 'X';

Or do I have this all wrong? I have been using JFrames as my main class in the past, and he has thrown in these mandatory passed variables to (I believe) keep us from doing so. Sorry for the craziness!

The best way to handle this situation would be to use the "Board" class to handle the data and have a sort of "Virtual" game board. Then use the JFrame specifically to show a physical copy of that virtual board to the user, so that the user may interact with the program. Doing this will allow all of the logic behind "Tic Tac Toe" To be left to the "Board" class, and any logic within the GUI will solely be to allow interaction between the user and the game.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM