• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 11:04
CEST 17:04
KST 00:04
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
Serral wins HomeStory Cup 2914Serral wins Maestros of the Game 243ByuL, and the Limitations of Standard Play3Team Liquid Map Contest #22: Results and Winners7Code S Season 2 (2026): RO4 and Finals Preview12
Community News
Balance hotfix patch 5.0.16b (July 16)22Reynor: GSL Loss Wasn't About Preparation Format16[IPSL] Spring 2026 Grand Finals - This Weekend!5Weekly Cups (July 6 - 12): Protoss strike back12BSL Season 22 Full Overview & Conclusion8
StarCraft 2
General
Balance hotfix patch 5.0.16b (July 16) Reynor: GSL Loss Wasn't About Preparation Format Is the larve respawn broken? 5.0.16 patch for SC2 goes live (8 worker start) BGE Stara Zagora to be held again in June 2025
Tourneys
Master Swan Open (Global Bronze-Master 2) WardiTV Summer Cup 2026 GSL CK #5 Race War RSL Revival: Season 6 - Qualifiers and Main Event HomeStory Cup 29
Strategy
[G] Having the right mentality to improve
Custom Maps
New Map Maker - Looking for Advice - Love or Hate Work In Progress Melee Maps [D]RTS in all its shapes and glory <3
External Content
The PondCast: SC2 News & Results Mutation # 534 Burning Evacuation Mutation # 533 Die Together Mutation # 532 Nuclear Family
Brood War
General
BW General Discussion Recommended FPV games (post-KeSPA) Etiquete rules in Asl? Pros Debate: Zerg Unfairly Nerfed? (ASL S22 map) BGH Auto Balance -> http://bghmmr.eu/
Tourneys
Escore Tournament - Season 3 Small VOD Thread 2.0 [IPSL] Spring 2026 Grand Finals - This Weekend! [Megathread] Daily Proleagues
Strategy
Fighting Spirit mining rates Simple Questions, Simple Answers Creating a full chart of Zerg builds Relatively freeroll strategies
Other Games
General Games
Path of Exile General RTS Discussion Thread Nintendo Switch Thread Beyond All Reason Stormgate/Frost Giant Megathread
Dota 2
Looking for a Dota Mentor Official 'what is Dota anymore' discussion
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug
TL Mafia
TL Mafia Power Rank NeO.D_StephenKing vs This Guy From 1 Million Dance TL Mafia Community Thread Vanilla Mini Mafia
Community
General
US Politics Mega-thread The Games Industry And ATVI Russo-Ukrainian War Thread UK Politics Mega-thread YouTube Thread
Fan Clubs
The IdrA Fan Club The HerO Fan Club!
Media & Entertainment
Movie Discussion! Anime Discussion Thread [Req][Books] Good Fantasy/SciFi books Series you have seen recently...
Sports
2024 - 2026 Football Thread MLB/Baseball 2023 McBoner: A hockey love story Tennis[sport] Formula 1 Discussion
World Cup 2022
Tech Support
Simple Questions Simple Answers FPS when play League Of Legend on laptop How to clean a TTe Thermaltake keyboard?
TL Community
Northern Ireland Global Starcraft The Automated Ban List
Blogs
Poker (part 2)
Nebuchad
The Experiences We Want and …
TrAiDoS
An Exploration of th…
waywardstrategy
Gauntlet SC2: A Retrospectiv…
Ctone23
ramps on octagon
StaticNine
Funny Nicknames
LUCKY_NOOB
Evil Gacha Games and the…
ffswowsucks
Customize Sidebar...

Website Feedback

Closed Threads



Active: 7338 users

The Big Programming Thread - Page 411

Forum Index > General Forum
Post a Reply
Prev 1 409 410 411 412 413 1032 Next
Thread Rules
1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution.
2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20)
3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible.
4. Use [code] tags to format code blocks.
Vilanoil
Profile Blog Joined July 2010
Germany47 Posts
December 20 2013 08:09 GMT
#8201
On December 20 2013 11:20 Cyx. wrote:
Show nested quote +
On December 20 2013 11:02 Maero wrote:
On December 20 2013 10:11 Cyx. wrote:
On December 20 2013 09:26 Maero wrote:
On December 20 2013 07:49 Vilanoil wrote:

int sumAge(PlayerList list){
//stuff
}

int sumAge(){
return sumAge(this);
}
// is called in main with
players.sumAge();

I'm not sure but it seems like that is a recursive call of sumAge() and that (this) is automatically referring to the list players.
Would be awesome if someone could explain this.


All this is really doing is giving you a shorthand to sum the ages of the PlayerList this method is being called from. Ex.

PlayerList a;
PlayerList b; // pretend these have stuff in it

a.sumAge(b); // performs the operation on PlayerList b (I assume summing up the ages...)
a.sumAge(); // calls the non-parameterized one, then falls back to the parameterized method

// Basically...

a.sumAge() == a.sumAge(a);


Let me know if that doesn't make sense! It seems like a strange implementation, but that is the literal way that it is working.


But I guess the question is... good lord, why, please why something so awfully confusing? players.sumAge() will call sumAge(this)... calling players.sumAge(this) within that function just means you end up calling sumAge(this, this) basically. It's literally just adding an extra definition and extra confusion to something that should be one method. I see absolutely zero sense in doing it this way instead of just writing sumAge() to do the actual summing of ages, and then not bother with the definition that has an extra parameter (which never even gets used).


We completely agree! But he asked for an explanation of how it was working, so that's what I provided
The note at the end about it being a strange implementation was alluding to your point - there's no real good reason to put it together in that way and either one or the other would work fine (depending on how the method interacts with other PlayerList age sums).


okie, cool =) I guess I just wasn't sure if it was like... a Java thing or something (I mostly use C++) or if it was as totally weird as it seemed. @Vilanoil: were you given the code like that (for your class) or did you write the whole PlayerList class yourself?


The given like that:

int sumAge(PlayerList list)
{
// *** your implementation ***
}

// *************************************************************************
// recursive computation of sum of games of all players in the list
int sumAge()
{
return sumAge(this);
}

All we had to do is filling the gaps and all methods are give in this way ... It was confusing for us too~
But i guess i will just forget about it since it seems pretty useless
Cyx.
Profile Joined November 2010
Canada806 Posts
December 20 2013 08:51 GMT
#8202
On December 20 2013 17:09 Vilanoil wrote:
Show nested quote +
On December 20 2013 11:20 Cyx. wrote:
On December 20 2013 11:02 Maero wrote:
On December 20 2013 10:11 Cyx. wrote:
On December 20 2013 09:26 Maero wrote:
On December 20 2013 07:49 Vilanoil wrote:

int sumAge(PlayerList list){
//stuff
}

int sumAge(){
return sumAge(this);
}
// is called in main with
players.sumAge();

I'm not sure but it seems like that is a recursive call of sumAge() and that (this) is automatically referring to the list players.
Would be awesome if someone could explain this.


All this is really doing is giving you a shorthand to sum the ages of the PlayerList this method is being called from. Ex.

PlayerList a;
PlayerList b; // pretend these have stuff in it

a.sumAge(b); // performs the operation on PlayerList b (I assume summing up the ages...)
a.sumAge(); // calls the non-parameterized one, then falls back to the parameterized method

// Basically...

a.sumAge() == a.sumAge(a);


Let me know if that doesn't make sense! It seems like a strange implementation, but that is the literal way that it is working.


But I guess the question is... good lord, why, please why something so awfully confusing? players.sumAge() will call sumAge(this)... calling players.sumAge(this) within that function just means you end up calling sumAge(this, this) basically. It's literally just adding an extra definition and extra confusion to something that should be one method. I see absolutely zero sense in doing it this way instead of just writing sumAge() to do the actual summing of ages, and then not bother with the definition that has an extra parameter (which never even gets used).


We completely agree! But he asked for an explanation of how it was working, so that's what I provided
The note at the end about it being a strange implementation was alluding to your point - there's no real good reason to put it together in that way and either one or the other would work fine (depending on how the method interacts with other PlayerList age sums).


okie, cool =) I guess I just wasn't sure if it was like... a Java thing or something (I mostly use C++) or if it was as totally weird as it seemed. @Vilanoil: were you given the code like that (for your class) or did you write the whole PlayerList class yourself?


The given like that:

int sumAge(PlayerList list)
{
// *** your implementation ***
}

// *************************************************************************
// recursive computation of sum of games of all players in the list
int sumAge()
{
return sumAge(this);
}

All we had to do is filling the gaps and all methods are give in this way ... It was confusing for us too~
But i guess i will just forget about it since it seems pretty useless


This is like... the weirdest fucking intro to recursion ever. You guys have talked about the implicit reference to "this" in method definitions right?
lannisport
Profile Joined February 2012
878 Posts
December 20 2013 09:32 GMT
#8203
Hey guys, I'm new to python and I'm trying to create a simple hangman game from one of my books. So far everything is good but I failed to recreate the part of the game that shows the dashes and the correct letters that you've guessed so far, such as PY---N I tried looking at the code example in the book but couldn't really understand how or why it works. I've copied the specific parts of the code that I don't understand.

I've commented each line to walk you through what I understand is happening.



so_far = "-" * len(word) # Creates dashes based on the length of the word

while wrong < MAX_WRONG and so_far != word:
print("\nSo far, the word is:\n", so_far) #Main loop, but I've cut out all the irrelevant stuff

guess = input("\n\nEnter your guess: ")

#Checking the Player's guess
if guess in word: #If the letter is in the word, let's say the word is PYTHON and the guess is O
print("\nYes!", guess, "is in the word!")

new = ""
for i in range(len(word)): #A sequence 0-6 is generated and this loop repeats 6 times
if guess == word[i]: #If the guess string matches the string from the index
new += guess # concatenate the guess string to whatever is in new
else:
new += so_far[i] #So new now becomes - in the first iteration
so_far = new #Once the loop ends so_far should look like ----O-



Am I understanding it right? And also... How do you even plan for this sorta thing? The book had an intro on pseudo code but I had never imagined to use a for loop and an if condition in this sort of combination.
Vilanoil
Profile Blog Joined July 2010
Germany47 Posts
December 20 2013 09:41 GMT
#8204
On December 20 2013 17:51 Cyx. wrote:
Show nested quote +
On December 20 2013 17:09 Vilanoil wrote:
On December 20 2013 11:20 Cyx. wrote:
On December 20 2013 11:02 Maero wrote:
On December 20 2013 10:11 Cyx. wrote:
On December 20 2013 09:26 Maero wrote:
On December 20 2013 07:49 Vilanoil wrote:

int sumAge(PlayerList list){
//stuff
}

int sumAge(){
return sumAge(this);
}
// is called in main with
players.sumAge();

I'm not sure but it seems like that is a recursive call of sumAge() and that (this) is automatically referring to the list players.
Would be awesome if someone could explain this.


All this is really doing is giving you a shorthand to sum the ages of the PlayerList this method is being called from. Ex.

PlayerList a;
PlayerList b; // pretend these have stuff in it

a.sumAge(b); // performs the operation on PlayerList b (I assume summing up the ages...)
a.sumAge(); // calls the non-parameterized one, then falls back to the parameterized method

// Basically...

a.sumAge() == a.sumAge(a);


Let me know if that doesn't make sense! It seems like a strange implementation, but that is the literal way that it is working.


But I guess the question is... good lord, why, please why something so awfully confusing? players.sumAge() will call sumAge(this)... calling players.sumAge(this) within that function just means you end up calling sumAge(this, this) basically. It's literally just adding an extra definition and extra confusion to something that should be one method. I see absolutely zero sense in doing it this way instead of just writing sumAge() to do the actual summing of ages, and then not bother with the definition that has an extra parameter (which never even gets used).


We completely agree! But he asked for an explanation of how it was working, so that's what I provided
The note at the end about it being a strange implementation was alluding to your point - there's no real good reason to put it together in that way and either one or the other would work fine (depending on how the method interacts with other PlayerList age sums).


okie, cool =) I guess I just wasn't sure if it was like... a Java thing or something (I mostly use C++) or if it was as totally weird as it seemed. @Vilanoil: were you given the code like that (for your class) or did you write the whole PlayerList class yourself?


The given like that:

int sumAge(PlayerList list)
{
// *** your implementation ***
}

// *************************************************************************
// recursive computation of sum of games of all players in the list
int sumAge()
{
return sumAge(this);
}

All we had to do is filling the gaps and all methods are give in this way ... It was confusing for us too~
But i guess i will just forget about it since it seems pretty useless


This is like... the weirdest fucking intro to recursion ever. You guys have talked about the implicit reference to "this" in method definitions right?


No we didn't cover anything like this. The only way we used "this" so far was actually in variable declaration in classes, eiter in the constructors or the class methods. ..
scudst0rm
Profile Joined May 2010
Canada1149 Posts
December 20 2013 19:28 GMT
#8205
On December 20 2013 18:32 lannisport wrote:
Hey guys, I'm new to python and I'm trying to create a simple hangman game from one of my books. So far everything is good but I failed to recreate the part of the game that shows the dashes and the correct letters that you've guessed so far, such as PY---N I tried looking at the code example in the book but couldn't really understand how or why it works. I've copied the specific parts of the code that I don't understand.

I've commented each line to walk you through what I understand is happening.

+ Show Spoiler +


so_far = "-" * len(word) # Creates dashes based on the length of the word

while wrong < MAX_WRONG and so_far != word:
print("\nSo far, the word is:\n", so_far) #Main loop, but I've cut out all the irrelevant stuff

guess = input("\n\nEnter your guess: ")

#Checking the Player's guess
if guess in word: #If the letter is in the word, let's say the word is PYTHON and the guess is O
print("\nYes!", guess, "is in the word!")

new = ""
for i in range(len(word)): #A sequence 0-6 is generated and this loop repeats 6 times
if guess == word[i]: #If the guess string matches the string from the index
new += guess # concatenate the guess string to whatever is in new
else:
new += so_far[i] #So new now becomes - in the first iteration
so_far = new #Once the loop ends so_far should look like ----O-



Am I understanding it right? And also... How do you even plan for this sorta thing? The book had an intro on pseudo code but I had never imagined to use a for loop and an if condition in this sort of combination.


It seems like you understand most of it. new += so_far[i] is basically saying "don't update this character in so_far"

I think it's a lot more intuitive to update so_far in place. This does the same thing but you can get rid of new:

for i in range(len(word)):
if guess == word[i]:
so_far[i] = word[i]


How do you plan this sort of thing? To me this is the most intuitive solution, i guess it just comes with experience.
You're like a one ranger army comin' at me...
Cyx.
Profile Joined November 2010
Canada806 Posts
December 20 2013 20:15 GMT
#8206
On December 21 2013 04:28 scudst0rm wrote:
Show nested quote +
On December 20 2013 18:32 lannisport wrote:
Hey guys, I'm new to python and I'm trying to create a simple hangman game from one of my books. So far everything is good but I failed to recreate the part of the game that shows the dashes and the correct letters that you've guessed so far, such as PY---N I tried looking at the code example in the book but couldn't really understand how or why it works. I've copied the specific parts of the code that I don't understand.

I've commented each line to walk you through what I understand is happening.

+ Show Spoiler +


so_far = "-" * len(word) # Creates dashes based on the length of the word

while wrong < MAX_WRONG and so_far != word:
print("\nSo far, the word is:\n", so_far) #Main loop, but I've cut out all the irrelevant stuff

guess = input("\n\nEnter your guess: ")

#Checking the Player's guess
if guess in word: #If the letter is in the word, let's say the word is PYTHON and the guess is O
print("\nYes!", guess, "is in the word!")

new = ""
for i in range(len(word)): #A sequence 0-6 is generated and this loop repeats 6 times
if guess == word[i]: #If the guess string matches the string from the index
new += guess # concatenate the guess string to whatever is in new
else:
new += so_far[i] #So new now becomes - in the first iteration
so_far = new #Once the loop ends so_far should look like ----O-



Am I understanding it right? And also... How do you even plan for this sorta thing? The book had an intro on pseudo code but I had never imagined to use a for loop and an if condition in this sort of combination.


It seems like you understand most of it. new += so_far[i] is basically saying "don't update this character in so_far"

I think it's a lot more intuitive to update so_far in place. This does the same thing but you can get rid of new:

for i in range(len(word)):
if guess == word[i]:
so_far[i] = word[i]


How do you plan this sort of thing? To me this is the most intuitive solution, i guess it just comes with experience.

Correct me if I'm wrong, but I think Python strings are immutable - meaning the way he has it so far is the way to go about it ^^ Testing in an interpreter gives:

>>>hello = "HelloWorld!"
>>>hello[2] = "a"
Traceback (most recent call last):
File stdin, line 1, in <module>
TypeError: 'str' object does not support item assignment
Maero
Profile Joined December 2007
349 Posts
Last Edited: 2013-12-21 01:42:30
December 21 2013 01:30 GMT
#8207
Yep. Any operations on a python string must result in a new string. Good illustration Cyx.

Edit: Just to contribute something: One way around this is to turn the string into a list instead, modify an element, and rejoin. e.g.

>>> str = "string"
>>> str = list(str)
>>> str[2] = 'a'
>>> str = ''.join(str)
>>> str
'staing'


But the way in your book is probably just about the same as far as efficiency goes, if not a little better.
Prillan
Profile Joined August 2011
Sweden350 Posts
December 21 2013 17:58 GMT
#8208
On December 21 2013 10:30 Maero wrote:
Yep. Any operations on a python string must result in a new string. Good illustration Cyx.

Edit: Just to contribute something: One way around this is to turn the string into a list instead, modify an element, and rejoin. e.g.

>>> str = "string"
>>> str = list(str)
>>> str[2] = 'a'
>>> str = ''.join(str)
>>> str
'staing'


But the way in your book is probably just about the same as far as efficiency goes, if not a little better.

Please don't use str as a name for a variable as it is a built-in function/type. By assigning a value to it you overwrite its default value which can cause all kinds of problems in a bigger program. You probably know this Maero but it might be worth to point it out to people learning Python.
TheBB's sidekick, aligulac.com | "Reality is frequently inaccurate." - Douglas Adams
Isualin
Profile Joined March 2011
Germany1903 Posts
December 21 2013 18:09 GMT
#8209
I am trying to write a program to crack zip passwords in c/c++ with mpi using brute force. I know how to distribute the work through mpi. There are a few libraries to unzip files like minizip, libzip etc. I will try to unzip the file using them with for loops and incrementing passwords in each process with different starting points, But... this seems really inefficient. Can anyone give me pointers?
| INnoVation | The literal god TY | ByuNjwa | LRSL when? |
WarSame
Profile Blog Joined February 2010
Canada1950 Posts
December 22 2013 02:56 GMT
#8210
Hello TL! I have started trying to create a TicTacToe game and ran into a problem with the graphics. Currently I can create the window and the background I like using fillRect to fill the background blue, and then fillRect to fill each square black, leaving blue bars in between. However, I would like help adding in something that redraws/draws over what exists when I click on the window. The graphics documentation is confusing the hell out of me. There is probably 1 call that needs to be made to do this, but I've trawled through a bunch of SO questions and the documentation and nothing has shown up.

My code is:

+ Show Spoiler +
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;

public class GraphicsStart extends JFrame implements MouseListener{
private static final long serialVersionUID = 1L;
final int boardDimensions = 600;//Pixel dimensions of the square board.
final int xDivisions = 3;//Brackets of each dimension.
final int yDivisions = 3;
final int frameWidth = 5;//Width of frame in pixels. Occurs on both sides, so double.

public GraphicsStart(){
setTitle("TicTacToe - Graeme Cliffe");
setSize(boardDimensions,boardDimensions);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
addMouseListener(this);
}

public void paint(Graphics g){
g.setColor(Color.BLUE);
g.fillRect(0, 0, boardDimensions, boardDimensions);//Creates the background.
g.setColor(Color.BLACK);
for (int xInc = 0; xInc < xDivisions; xInc++){//Increments through the Xs
for (int yInc = 0; yInc <yDivisions; yInc++){
g.fillRect(xInc*(boardDimensions/xDivisions), yInc*(boardDimensions/yDivisions),
xInc + (boardDimensions/xDivisions)-frameWidth, yInc +(boardDimensions/yDivisions)-frameWidth);
//Each box is 195x195, and there are currently 9 of them.
}
}

}

public void mouseClicked(MouseEvent e){
//This is where the magic should happen but what should be called? Ideally I would draw over the old image with some new rectangle.
int xClick = e.getX();
int yClick = e.getY();
repaint();
}

public static void main(String args[]) {
GraphicsStart demo = new GraphicsStart();
}

@Override
public void mouseEntered(MouseEvent arg0) {

}

@Override
public void mouseExited(MouseEvent arg0) {

}

@Override
public void mousePressed(MouseEvent arg0) {

}

@Override
public void mouseReleased(MouseEvent arg0) {

}


}
Can it be I stayed away too long? Did you miss these rhymes while I was gone?
betaflame
Profile Joined November 2010
175 Posts
December 22 2013 06:40 GMT
#8211
On December 22 2013 03:09 Isualin wrote:
I am trying to write a program to crack zip passwords in c/c++ with mpi using brute force. I know how to distribute the work through mpi. There are a few libraries to unzip files like minizip, libzip etc. I will try to unzip the file using them with for loops and incrementing passwords in each process with different starting points, But... this seems really inefficient. Can anyone give me pointers?


Well, given that you are trying to crack the password with brute force, I can't imagine there would be an "efficient" method of brute forcing since brute forcing is by nature inefficient.
aksfjh
Profile Joined November 2010
United States4853 Posts
December 22 2013 07:16 GMT
#8212
On December 22 2013 15:40 betaflame wrote:
Show nested quote +
On December 22 2013 03:09 Isualin wrote:
I am trying to write a program to crack zip passwords in c/c++ with mpi using brute force. I know how to distribute the work through mpi. There are a few libraries to unzip files like minizip, libzip etc. I will try to unzip the file using them with for loops and incrementing passwords in each process with different starting points, But... this seems really inefficient. Can anyone give me pointers?


Well, given that you are trying to crack the password with brute force, I can't imagine there would be an "efficient" method of brute forcing since brute forcing is by nature inefficient.

Not entirely. Brute forcing just means blind guessing, iterating through all options. There are still ways to be efficient.

Onto a problem I'm having, anybody know of a good way to create a string based on Perl regex embedded into a normal string? Tried String::Random, but it only creates a string if the input is solely regex.
xboi209
Profile Blog Joined June 2011
United States1173 Posts
Last Edited: 2013-12-22 08:09:40
December 22 2013 07:31 GMT
#8213
Nvm
http://www.reddit.com/r/broodwar/
bangsholt
Profile Joined June 2011
Denmark138 Posts
December 22 2013 09:15 GMT
#8214
On December 22 2013 11:56 WarSame wrote:
Hello TL! I have started trying to create a TicTacToe game and ran into a problem with the graphics. Currently I can create the window and the background I like using fillRect to fill the background blue, and then fillRect to fill each square black, leaving blue bars in between. However, I would like help adding in something that redraws/draws over what exists when I click on the window. The graphics documentation is confusing the hell out of me. There is probably 1 call that needs to be made to do this, but I've trawled through a bunch of SO questions and the documentation and nothing has shown up.

My code is:

+ Show Spoiler +
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;

public class GraphicsStart extends JFrame implements MouseListener{
private static final long serialVersionUID = 1L;
final int boardDimensions = 600;//Pixel dimensions of the square board.
final int xDivisions = 3;//Brackets of each dimension.
final int yDivisions = 3;
final int frameWidth = 5;//Width of frame in pixels. Occurs on both sides, so double.

public GraphicsStart(){
setTitle("TicTacToe - Graeme Cliffe");
setSize(boardDimensions,boardDimensions);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
addMouseListener(this);
}

public void paint(Graphics g){
g.setColor(Color.BLUE);
g.fillRect(0, 0, boardDimensions, boardDimensions);//Creates the background.
g.setColor(Color.BLACK);
for (int xInc = 0; xInc < xDivisions; xInc++){//Increments through the Xs
for (int yInc = 0; yInc <yDivisions; yInc++){
g.fillRect(xInc*(boardDimensions/xDivisions), yInc*(boardDimensions/yDivisions),
xInc + (boardDimensions/xDivisions)-frameWidth, yInc +(boardDimensions/yDivisions)-frameWidth);
//Each box is 195x195, and there are currently 9 of them.
}
}

}

public void mouseClicked(MouseEvent e){
//This is where the magic should happen but what should be called? Ideally I would draw over the old image with some new rectangle.
int xClick = e.getX();
int yClick = e.getY();
repaint();
}

public static void main(String args[]) {
GraphicsStart demo = new GraphicsStart();
}

@Override
public void mouseEntered(MouseEvent arg0) {

}

@Override
public void mouseExited(MouseEvent arg0) {

}

@Override
public void mousePressed(MouseEvent arg0) {

}

@Override
public void mouseReleased(MouseEvent arg0) {

}


}


It's "simple"

First you need to figure out which square you are in, then you draw exactly like before, just with a new color
FakePseudo
Profile Joined January 2012
Belgium716 Posts
December 22 2013 11:33 GMT
#8215
On December 21 2013 10:30 Maero wrote:
Yep. Any operations on a python string must result in a new string. Good illustration Cyx.

Edit: Just to contribute something: One way around this is to turn the string into a list instead, modify an element, and rejoin. e.g.

>>> str = "string"
>>> str = list(str)
>>> str[2] = 'a'
>>> str = ''.join(str)
>>> str
'staing'


But the way in your book is probably just about the same as far as efficiency goes, if not a little better.


How about:
newString=oldString[:2]+"a"+oldString[3:]

I am the 0.0007% /forum/viewpost.php?post_id=17208334|| Big Black Women Vocals Is Like Porn to my Ears ||San Antonio Spurs|Boston Celtics||#1EZToss Hater;
WarSame
Profile Blog Joined February 2010
Canada1950 Posts
December 22 2013 17:39 GMT
#8216
On December 22 2013 18:15 bangsholt wrote:
Show nested quote +
On December 22 2013 11:56 WarSame wrote:
Hello TL! I have started trying to create a TicTacToe game and ran into a problem with the graphics. Currently I can create the window and the background I like using fillRect to fill the background blue, and then fillRect to fill each square black, leaving blue bars in between. However, I would like help adding in something that redraws/draws over what exists when I click on the window. The graphics documentation is confusing the hell out of me. There is probably 1 call that needs to be made to do this, but I've trawled through a bunch of SO questions and the documentation and nothing has shown up.

My code is:

+ Show Spoiler +
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;

public class GraphicsStart extends JFrame implements MouseListener{
private static final long serialVersionUID = 1L;
final int boardDimensions = 600;//Pixel dimensions of the square board.
final int xDivisions = 3;//Brackets of each dimension.
final int yDivisions = 3;
final int frameWidth = 5;//Width of frame in pixels. Occurs on both sides, so double.

public GraphicsStart(){
setTitle("TicTacToe - Graeme Cliffe");
setSize(boardDimensions,boardDimensions);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
addMouseListener(this);
}

public void paint(Graphics g){
g.setColor(Color.BLUE);
g.fillRect(0, 0, boardDimensions, boardDimensions);//Creates the background.
g.setColor(Color.BLACK);
for (int xInc = 0; xInc < xDivisions; xInc++){//Increments through the Xs
for (int yInc = 0; yInc <yDivisions; yInc++){
g.fillRect(xInc*(boardDimensions/xDivisions), yInc*(boardDimensions/yDivisions),
xInc + (boardDimensions/xDivisions)-frameWidth, yInc +(boardDimensions/yDivisions)-frameWidth);
//Each box is 195x195, and there are currently 9 of them.
}
}

}

public void mouseClicked(MouseEvent e){
//This is where the magic should happen but what should be called? Ideally I would draw over the old image with some new rectangle.
int xClick = e.getX();
int yClick = e.getY();
repaint();
}

public static void main(String args[]) {
GraphicsStart demo = new GraphicsStart();
}

@Override
public void mouseEntered(MouseEvent arg0) {

}

@Override
public void mouseExited(MouseEvent arg0) {

}

@Override
public void mousePressed(MouseEvent arg0) {

}

@Override
public void mouseReleased(MouseEvent arg0) {

}


}


It's "simple"

First you need to figure out which square you are in, then you draw exactly like before, just with a new color

Well that's the problem. I can figure out which square was clicked on no problem! But then I have no idea what you are supposed to use to redraw over the old one.

Ideally the pseudocode would be:

public void repaint(){
getOldPaint();
newPaint = combineOldAndNewPaint();
paintOver(newPaint);
}

But I don't know any of those commands in Java's Graphics or Swing and haven't been able to find them through trawling for an hour or 2.

So I decided to switch over to something I saw used in a guide, which was to have a JFrame and JPanel class. Hopefully this works better.
Can it be I stayed away too long? Did you miss these rhymes while I was gone?
bangsholt
Profile Joined June 2011
Denmark138 Posts
December 22 2013 17:57 GMT
#8217
On December 23 2013 02:39 WarSame wrote:
Show nested quote +
On December 22 2013 18:15 bangsholt wrote:
On December 22 2013 11:56 WarSame wrote:
Hello TL! I have started trying to create a TicTacToe game and ran into a problem with the graphics. Currently I can create the window and the background I like using fillRect to fill the background blue, and then fillRect to fill each square black, leaving blue bars in between. However, I would like help adding in something that redraws/draws over what exists when I click on the window. The graphics documentation is confusing the hell out of me. There is probably 1 call that needs to be made to do this, but I've trawled through a bunch of SO questions and the documentation and nothing has shown up.

My code is:

+ Show Spoiler +
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;

public class GraphicsStart extends JFrame implements MouseListener{
private static final long serialVersionUID = 1L;
final int boardDimensions = 600;//Pixel dimensions of the square board.
final int xDivisions = 3;//Brackets of each dimension.
final int yDivisions = 3;
final int frameWidth = 5;//Width of frame in pixels. Occurs on both sides, so double.

public GraphicsStart(){
setTitle("TicTacToe - Graeme Cliffe");
setSize(boardDimensions,boardDimensions);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
addMouseListener(this);
}

public void paint(Graphics g){
g.setColor(Color.BLUE);
g.fillRect(0, 0, boardDimensions, boardDimensions);//Creates the background.
g.setColor(Color.BLACK);
for (int xInc = 0; xInc < xDivisions; xInc++){//Increments through the Xs
for (int yInc = 0; yInc <yDivisions; yInc++){
g.fillRect(xInc*(boardDimensions/xDivisions), yInc*(boardDimensions/yDivisions),
xInc + (boardDimensions/xDivisions)-frameWidth, yInc +(boardDimensions/yDivisions)-frameWidth);
//Each box is 195x195, and there are currently 9 of them.
}
}

}

public void mouseClicked(MouseEvent e){
//This is where the magic should happen but what should be called? Ideally I would draw over the old image with some new rectangle.
int xClick = e.getX();
int yClick = e.getY();
repaint();
}

public static void main(String args[]) {
GraphicsStart demo = new GraphicsStart();
}

@Override
public void mouseEntered(MouseEvent arg0) {

}

@Override
public void mouseExited(MouseEvent arg0) {

}

@Override
public void mousePressed(MouseEvent arg0) {

}

@Override
public void mouseReleased(MouseEvent arg0) {

}


}


It's "simple"

First you need to figure out which square you are in, then you draw exactly like before, just with a new color

Well that's the problem. I can figure out which square was clicked on no problem! But then I have no idea what you are supposed to use to redraw over the old one.

Ideally the pseudocode would be:

public void repaint(){
getOldPaint();
newPaint = combineOldAndNewPaint();
paintOver(newPaint);
}

But I don't know any of those commands in Java's Graphics or Swing and haven't been able to find them through trawling for an hour or 2.

So I decided to switch over to something I saw used in a guide, which was to have a JFrame and JPanel class. Hopefully this works better.


You do exactly the same - you just draw "on top" of the old graphics and it replaces it
Zocat
Profile Joined April 2010
Germany2229 Posts
Last Edited: 2013-12-22 18:38:33
December 22 2013 18:35 GMT
#8218
repaint just calls the normal paint method. So change that one. Dont implement your own repaint.
You dont paint over something. You clear everything and paint from scratch.

i.e. after your 2 for loops change to another color. And then g.fillRect with the parameters of the clicked box:
g.setColor(Color.RED);
g.fillRect(0*(boardDimensions/xDivisions), 0*(boardDimensions/yDivisions),
0 + (boardDimensions/xDivisions)-frameWidth, 0 +(boardDimensions/yDivisions)-frameWidth);
If the clicked one is the (0,0) one.

You can also do this change in the initial for loops and check if the current one is the one which is clicked and change colors accordingly.
if (xInc == xClicked && yInc == yClicked) g.setColor(Color.RED);
// draw your normal rectangle as before
g.setColor(Color.BLACK);
]343[
Profile Blog Joined May 2008
United States10328 Posts
December 22 2013 19:29 GMT
#8219
I did my first ever TopCoder contest today and got blue!

On the other hand, I failed at coding a binary search (to find the rightmost element strictly less than the query) for like 15 minutes! :D :D
Writer
Maindi
Profile Joined November 2011
Finland104 Posts
December 22 2013 20:01 GMT
#8220
On December 23 2013 02:39 WarSame wrote:
Show nested quote +
On December 22 2013 18:15 bangsholt wrote:
On December 22 2013 11:56 WarSame wrote:
Hello TL! I have started trying to create a TicTacToe game and ran into a problem with the graphics. Currently I can create the window and the background I like using fillRect to fill the background blue, and then fillRect to fill each square black, leaving blue bars in between. However, I would like help adding in something that redraws/draws over what exists when I click on the window. The graphics documentation is confusing the hell out of me. There is probably 1 call that needs to be made to do this, but I've trawled through a bunch of SO questions and the documentation and nothing has shown up.

My code is:

+ Show Spoiler +
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;

public class GraphicsStart extends JFrame implements MouseListener{
private static final long serialVersionUID = 1L;
final int boardDimensions = 600;//Pixel dimensions of the square board.
final int xDivisions = 3;//Brackets of each dimension.
final int yDivisions = 3;
final int frameWidth = 5;//Width of frame in pixels. Occurs on both sides, so double.

public GraphicsStart(){
setTitle("TicTacToe - Graeme Cliffe");
setSize(boardDimensions,boardDimensions);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
addMouseListener(this);
}

public void paint(Graphics g){
g.setColor(Color.BLUE);
g.fillRect(0, 0, boardDimensions, boardDimensions);//Creates the background.
g.setColor(Color.BLACK);
for (int xInc = 0; xInc < xDivisions; xInc++){//Increments through the Xs
for (int yInc = 0; yInc <yDivisions; yInc++){
g.fillRect(xInc*(boardDimensions/xDivisions), yInc*(boardDimensions/yDivisions),
xInc + (boardDimensions/xDivisions)-frameWidth, yInc +(boardDimensions/yDivisions)-frameWidth);
//Each box is 195x195, and there are currently 9 of them.
}
}

}

public void mouseClicked(MouseEvent e){
//This is where the magic should happen but what should be called? Ideally I would draw over the old image with some new rectangle.
int xClick = e.getX();
int yClick = e.getY();
repaint();
}

public static void main(String args[]) {
GraphicsStart demo = new GraphicsStart();
}

@Override
public void mouseEntered(MouseEvent arg0) {

}

@Override
public void mouseExited(MouseEvent arg0) {

}

@Override
public void mousePressed(MouseEvent arg0) {

}

@Override
public void mouseReleased(MouseEvent arg0) {

}


}


It's "simple"

First you need to figure out which square you are in, then you draw exactly like before, just with a new color

Well that's the problem. I can figure out which square was clicked on no problem! But then I have no idea what you are supposed to use to redraw over the old one.

Ideally the pseudocode would be:

public void repaint(){
getOldPaint();
newPaint = combineOldAndNewPaint();
paintOver(newPaint);
}

But I don't know any of those commands in Java's Graphics or Swing and haven't been able to find them through trawling for an hour or 2.

So I decided to switch over to something I saw used in a guide, which was to have a JFrame and JPanel class. Hopefully this works better.

Probably an easier and more elegant way to do the game would be creating a grid (for example a two dimensional vector) and do the actual playing there.
Prev 1 409 410 411 412 413 1032 Next
Please log in or register to reply.
Live Events Refresh
Showmatch
14:30
WardiTV Showmatch #1
Clem vs herOLIVE!
WardiTV0
Liquipedia
Epic.LAN
13:00
Epic.LAN 48 Group Stage
epiclan53
Liquipedia
CrankTV Team League
11:00
Crank Gathers S4: Group Stage
LiquipediaDiscussion
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
RotterdaM 49
RushiSC 44
DenverSC2 11
StarCraft: Brood War
Calm 8327
GuemChi 2283
Rain 2268
Jaedong 1350
EffOrt 763
Hyuk 467
firebathero 366
Mini 365
Snow 342
BeSt 261
[ Show more ]
ZerO 240
Stork 221
Rush 217
Larva 159
ggaemo 128
Zeus 124
Dewaltoss 106
Hyun 81
Sea.KH 68
Mong 63
hero 57
Free 43
soO 39
Sharp 37
scan(afreeca) 36
sorry 32
Terrorterran 30
ToSsGirL 29
Barracks 26
JYJ 23
NaDa 20
Sexy 20
Bale 19
Hm[arnc] 17
Sacsri 16
Rock 15
Noble 15
IntoTheRainbow 12
yabsab 11
ajuk12(nOOB) 11
Purpose 8
Dota 2
Gorgc9488
syndereN260
Trikslyr27
League of Legends
Doublelift2517
Counter-Strike
byalli1043
fl0m609
allub252
kRYSTAL_24
Heroes of the Storm
Khaldor0
Other Games
singsing1870
B2W.Neo779
hiko727
crisheroes339
XaKoH 177
Liquid`VortiX172
ToD170
QueenE75
Rex16
ZerO(Twitch)15
Organizations
Other Games
gamesdonequick2353
BasetradeTV216
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
[ Show 15 non-featured ]
StarCraft 2
• poizon28 3
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• escodisco4317
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Dota 2
• C_a_k_e 1450
League of Legends
• Jankos2411
• TFBlade826
Upcoming Events
Big Brain Bouts
57m
SHIN vs Elazer
Percival vs Nicoract
Reynor vs Lambo
Replay Cast
8h 57m
RSL Revival
17h 57m
Clem vs Lambo
Scarlett vs Cure
CranKy Ducklings
18h 57m
Epic.LAN
21h 57m
IPSL
1d
Dragon vs Hawk
RSL Revival
1d 17h
Classic vs Trap
herO vs SHIN
Sparkling Tuna Cup
1d 18h
OSC
1d 21h
IPSL
2 days
Bonyth vs Ret
[ Show More ]
WardiTV Weekly
2 days
Monday Night Weeklies
3 days
PiGosaur Cup
4 days
The PondCast
4 days
Replay Cast
5 days
CrankTV Team League
5 days
Replay Cast
6 days
CrankTV Team League
6 days
Liquipedia Results

Completed

Proleague 2026-07-13
HSC XXIX
Eternal Conflict S2 E2

Ongoing

IPSL Spring 2026
Acropolis #4
YSL S3
CSL 2026 Summer (S21)
KCM Race Survival 2026 Season 3
Escore Tournament S3: W3
RSL Revival: Season 6
CranK Gathers Season 4: BW vs SC2 Team League
SCTL 2026 Spring
Stake Ranked Episode 3
XSE Pro League 2026
IEM Cologne Major 2026
Stake Ranked Episode 2
CS Asia Championships 2026
Asian Champions League 2026
IEM Atlanta 2026
PGL Astana 2026

Upcoming

ASL S22 SEASON OPEN Day 1
Escore Tournament S3: W4
ASL S22 SEASON OPEN Day 2
Escore Tournament S3: W5
CSLAN 4
Blizzard Classic Cup 2026
HSC XXX
SC4ALL II: StarCraft II
Kung Fu Cup 2026 Grand Finals
Light Tournament 2026
Eternal Conflict S2 Finale
Eternal Conflict S2 E3
Logitech G Connect 2026
StarSeries Fall 2026
FISSURE Playground #5
BLAST Open Fall 2026
Esports World Cup 2026
BLAST Bounty Summer 2026
BLAST Bounty Summer Qual
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2026 TLnet. All Rights Reserved.