46

First of all, let me join others in pointing out what sweet ASCII art you've done. This definitely has the beginnings of a much nicer text game than most. I'd start by defining some structures to hold data about specific things in the game. For a couple of examples: struct Ability { std::string name; int level_adder; int cost(int level, int ...


41

You're not breaking up the patterns in the most logical way. Each line just consists of repeating blocks of three characters. public class SquigglePlus { private static final String[] PATTERNS = { "~~~", "~+~", "+~+", "~~~" }; public static void main(String[] args) { for (int line = 0; line < PATTERNS.length; line++) { for (...


32

Repetitions All your elif share the same structure. You just need to ensure that the inserted pattern is always 3 wide and you can drop your offsets adjustments. The logical next step is to use a dictionary to store the pattern and the row for each attempt: _GOWN_MODIFIER = { 1: (' O ', 2), 2: (' | ', 3), 3: ('/| ', 3), 4: ('/|\\', 3), ...


31

Lots of answers already, but most of them seem to be about alternative solutions. I'll review your code on the details and show you how to refactor your code step by step. (This is a long answer, link to bottom of answer) String s = new String(); Creating a new String could be simplified to "". The only time you'll actually use the String constructor is ...


30

Here is a lovely way to do it using only one nested for-loop: for (int i = 0; i < 7; i++) { for (int numStars = 0; numStars < 4 - Math.abs(3 - i); numStars++) { System.out.print("*"); } System.out.println(); } This uses the Math.abs function to perform the calculation of how many stars to print. If we take a look at a plot of the ...


29

There's good news and bad news about this code. The bad news is that it's not very good code. The good news is that it's not going to be too hard to improve it greatly, and the ASCII art is awesome! Here are some observations that may help you improve your code. Don't abuse using namespace std Putting using namespace std at the top of every program is a ...


20

for (int i = 1; i <= 4; i++){ for (int j = 1; j <= 12; j++){ System.out.print("~"); } } System.out.println(); can easily be: for (int i = 0; i < 48; i++) { System.out.print('~'); } System.out.println(); Some edits: Improved formatting: ){ to ) { Simplified to one for loop that runs from 0 to 47 (start at 0, run to less than ...


17

First of all, in my opinion, both the algorithm is nice and interesting, and the code is really well-written, broken down into easily understandable functions! Well done! The only addition that I can make are corner-cases (and their possible fixes) for some of the helper functions. However, please note, that from the main entry point, I did not find any way ...


16

You've hard-coded 3 in three places. Yet, the output contains a row with four stars. That's underhanded programming. The culprit is this line: for(int j=0; j<=i; j++) Idiomatic Java would be either for (int j = 0; j < somelimit; j++) or for (int j = 1; j <= somelimit; j++) I also find the way that you interchanged i and j between the first ...


16

Control Flow I want to endorse and flesh out a bit Edward’s advice on removing the goto statements. A better approach here is some kind of state machine. You’ve got some kind of game state that tracks what mode you’re in, such as whether you’re selecting an ability, moving around, starting an encounter, and so on. You draw whatever screen is appropriate ...


16

Read PEP8, it will give you directions on how to write Python code that look like Python code to other. Other than that, the behaviour you're seeking is already implemented in range: >>> a = range(7, 0, -2) >>> list(a) [7, 5, 3, 1] You just need to reverse it to form the full hourglass: >>> a = range(7, 0, -2) >>> list(...


15

for (int i = 0; i < 10; i++) { System.out.println("XXXXXXXXXXOOOOOOOOO".substring(9 - i, 19 - i)); } This uses a string of 10 Xs and 9 Os It suggests that you could do: for (int i = 0; i < 10; i++) { int xs = 1 + i; int os = 9 - i; // xs + os == 10 ... call System.out.print("X"); in a loop ... call System.out.print("O"); in a ...


12

You have a working program that does what you wanted, so that's an excellent start for a beginner! I have a couple of suggestions that may help you improve your program. Consider using a data structure Right now, the data for the digits and the code to print them is intertwined. Since the data for the large digits is just data, consider separating that ...


12

General things You have two loops like this: for (int i = 1; i <= 4; i++){ for (int j = 1; j <= 12; j++){ System.out.print("~"); } } This is just nesting a loop for the sake of nesting a loop. I understand that the requirements state to use nested loops, but you should not make code purposefully inefficient just for the sake of it. ...


12

Before going into any coding I would give you a quick hack for the diamond problem . Instead of making so many for looping and columns and keeping track of variables the diamond problem is a variant of a maths problem. HERE 1*1 = 1 11*11 = 121 111*111 = 12321 1111*1111 = 1234321 and so on. SO if you use this formula you just need one for loop for spaces ...


12

You might want to load the ascii art from separate text files so you can edit those without messing with the functional code.


11

You can test for emptiness of number by just if not number After testing for emptiness, you could go straight to an int by adding number = int(number) Inside the loops, when you test for i % 2 == 0:, and then put None as the loop body (that should really be pass), you could just test for if i % 2 != 0:, and replace None with the else body, and remove the ...


11

When presented with problems like this it is common to decode/encode the input characters once, and store the decoded output in a structure that helps you on the presentation layer. First, though your control loops and input management... User Interaction I recommend you create a single class to encapsulate the Scanner instance that should only be created ...


11

You are doing a lot of if-condition to find what you need to print. This works but this is a verbose. You could find some logic in what you want to print. Your output is a simple square matrix that you want to split by it's diagonal. Here is the matrix : 0x0 0-1 0-2 0-3 0-4 0-5 0-6 0-7 0-8 0-9 1-0 1x1 1-2 1-3 1-4 1-5 1-6 ...


10

My particular concerns are the number of parameters I have going to the inner methods. How can those be rationalized? That's a tough nut, the only idea I have is to aggregate them somehow. I couldn't find any strong relationship between the parameters, e.g., like that the two first always work together, so I don't know. Should they be? Probably yes... ...


10

Wells, for the sake of loop-ifying everything, you may want to consider changing boundary as such: final char[] boundary = "=*=*=*".toCharArray(); ... sb.append( buildLines( '+', boundary, 3, NONE, 0, Direction.CENTER, 1, 2 ) ); That will create =*=*=*, duplicate it once, and add the + character at both ends. The ternary operator below looks like a good ...


10

I see a number of things that could help you improve your code. Don't disregard readability Readability of code is always of use and always of interest, even if you're the only one who ever sees the code. Since, by definition, you're asking for a code review by posting here, even that assumption is clearly faulty, because every reviewer is going to have ...


10

Generator expressions for loops like this that are simply appending items to lists are a little ugly: for i in xrange(h-1): output.append('') This is not ideal. Rather, you can directly set the value of output like this: output = ['' for _ in xrange(h - 1)] The generator expression pattern can also be applied to this block of code as well: for d in ...


10

Patterns. Aren't they lovely? There are two things I don't like in your code: You're using a lot of repeated System.out.print calls which does have some overhead (might be considered premature optimization, but I think it is good to know about this) You are not making as much use of the patterns as you could be. Let's take a look at your current code: ...


10

The code is not self-documenting, because the problem description is non-trivial and involves what you'd call "business logic". That is, you're not trying to perform some technical operation (like a cache, or a data structure, or a parser), but you're following rules defined by someone else. Each of those rules has to be programmed in, of course, but without ...


10

The roll function generates a random sequence of integers. The loop body shows me that you know how to seed a pseudo-RNG with a true source of randomness but you're doing it for every iteration. You should seed the pseudo-RNG once and then use it in the loop. You know how big your rand vector will be by the time you're done with it so you should reserve ...


10

You should generally try to avoid global variables (like tableSize). Since they can be used everywhere ("globally"), at the worst case you need to read all of the code to figure out where they come from and how they're used. In this case, tableSize could become a parameter of multTable. While it will probably work on the compilers you'll come across, void ...


9

It's ok for a first iteration. But your 2nd iteration should be drawRectangle(int width, int height). That will force you to not hard-code your numbers. Your 3rd iteration should be noticing that your rectangle only has 2 different rows (one row fills up the width with * and the other only has * in the beginning and end). You might want to make a method ...


9

Overall, your code is quite good. But there's always room for improvement: Syntax & Spelling Your question is tagged as python-2.7 so use print as a statement rather than as a function, or, better yet, use from __future__ import print_function to gain access to this feature. Indexing with magic numbers, such as coords[1], is ugly. Prefer sequence ...


8

Macro vs functions Do not use #define to implement functions. Write real functions with actual parameters. You'll avoid all sorts of problems. Function-like macros have their use, but as a beginner you should always prefer functions. Mind you, learning the pitfalls of function-like macros is good. Macro If you were to keep macros instead of functions (I ...


Only top voted, non community-wiki answers of a minimum length are eligible